0

I have put together this code and was wondering how to call certain data from a .config or .ini file. I have looked into AppSettings and ConfigurationManager but it’s just too much for the noob brain to compute :-)

I have added the system.configuration reference class to the references class list. Basically I want the variables for the MySQL stored on an external file that can be changed by an end user at a later time. I Could not put the whole code because it said I have exceeded 3000 lines or something.

DBServer = RemoteServer

dataBase = myDatabase

user = root

password = [the password here]

I have tried the following code with app.config but still nothing

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="constring" connectionString="server=localhost; database=SchoolDB; user     id=root; password=Pa55w0rd;"/>
  </connectionStrings>
</configuration>

Replaced the following in the Mainwindow.cs

string constring = "datasource=localhost;port=3306;username=root;password=Pa55w0rd";

with this

string constring = ConfigurationManager.ConnectionStrings["dbconnectionstring"].ConnectionString;

Then I get this error

"Object reference not set to an instance of an Object"

Ok so here is the Mainwindow.cs

public partial class Portal : Form
{      
    public Portal(string UserName)
    {
        InitializeComponent();
        lbl_username.Text = UserName;
        timer1.Start();
        txt_staff_password1.PasswordChar = '*';
        load_loan_laptops();            
    }
    DataTable dbdataset;

    void load_loan_laptops()
    {
        //string constring = "datasource=localhost;port=3306;username=root;    password=Pa55w0rd";
        string constring =     ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        string Query = "Select * from schooldb.loans where Avail = 'yes';";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                string devices = myReader.GetString("Device_Name");
                cmb_acer_loan.Items.Add(devices);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        conDataBase.Close();
    }

Please can someone help with this issue.

user3051236
  • 1
  • 1
  • 2

1 Answers1

0

problem : Your app.config file has <connectionString> tag instead of <connectionStrings> tag.

Solution: Replace <connectionString> with <connectionStrings> and </connectionString> with </connectionStrings> in app.config file

Try This:

app.config

  <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="constring" connectionString="datasource=localhost;port=3306;username=root;password=Pa55w0rd"/>
      </connectionStrings>
</configuration>

code:

string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67