0

When I add a Car in my application I get

The ConnectionString property has not been initialized.

I have the problem of ConnectionString property. I check similar question of mine but I found nothing helpfulness.

I use a class connection named dbConnection.cs:

class dbConnection { //Connection to database private string con = "Data Source=(local)\\SQLEXPRESS; Initial Catalog=MLQ7024; Integrated Security=TRUE".ToString(); public string Con { get { return con; } } } 

This is the code of my button

private void btnAddCar_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(dc.Con)) { DataTable dtCar = new DataTable(); BindingSource Car_bs = new BindingSource(); using (SqlCommand cmd = new SqlCommand("sp_Add_Car", con)) { try { cmd.CommandType = CommandType.StoredProcedure; //...... con.Open(); cmd.ExecuteNonQuery(); con.Close(); dtCar.Clear(); da.Fill(dtCar); } catch (Exception ex) { MessageBox.Show(ex.Message + "\t" + ex.Source); } } } refreshCar(); } 

This is the code of an another button working well without error

private void btnAddPayment_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(dc.Con)) { DataTable dtPayment = new DataTable(); using (SqlCommand cmd = new SqlCommand("sp_Add_Paiements", con)) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@id_paiement", SqlDbType.Char).Value = txtBoxPaymentId.Text; cmd.Parameters.Add("@montant", SqlDbType.SmallMoney).Value = txtBoxAmount.Text; cmd.Parameters.Add("@id_Location", SqlDbType.Char).Value = cmbpaymentLesaseId.Text; //cmd.Parameters.Add("@status", SqlDbType.Char).Value = txtBoxStatusPayment.Text; con.Open(); cmd.ExecuteNonQuery(); con.Close(); dtPayment.Clear(); da.Fill(dtPayment); btnAddLease.Hide(); refreshPayments(); } catch (Exception ex) { MessageBox.Show(ex.Message + "\t" + ex.Source); } } } btnAddPayment.Hide(); } 
15
  • Please provide info about dc.Con. Commented Oct 24, 2011 at 16:13
  • so what is dc.Con? For that matter, what is dc? Is it some static class with your connection information? Is it a non-static class? Commented Oct 24, 2011 at 16:13
  • dc is the connection?? did you add the connection string to it? Commented Oct 24, 2011 at 16:13
  • try to DEBUG. what is the value of dc.Con at runtime? Commented Oct 24, 2011 at 16:13
  • 1
    @ Frank - When you step through the code @ what line does it throw the error? Commented Oct 24, 2011 at 16:44

3 Answers 3

1

You aren't showing where you have initialized your dbConnection class. Changing it all to static will probably help, I'm guessing:

static class dbConnection { //Connection to database private static string con = "Data Source=(local)\\SQLEXPRESS; Initial Catalog=MLQ7024; Integrated Security=TRUE" public static string Con { get { return con; } } } 

If your dbConnection class worked in one method but not the other, chances are you had it initialized in one and not the other. Unless you have to deal with different database connections, using a static class for your database connections is probably the best route.

Then you change your calling method like this:

using (SqlConnection con = new SqlConnection(dbConnection.Con)) { // blah-blah } 
Sign up to request clarification or add additional context in comments.

9 Comments

He said he initialized it at the start of the form in the comment section.
@KreepN He's claiming that, but the error message is pretty clear cut that it didn't happen for that second method.
Yea, Id agree with your suggestion, but it looks as though he's using a tableadapter in his code to fill his datatable. I was curious as to if the tableadapter (which has a connection string) was possibly the cause of the error. As the second snippet doesn't use a TA.
dbConnection dc = new dbConnection(); error = Error 1 Cannot declare a variable of static type
Remove the declaration and use: using (SqlConnection con = new SqlConnection(dbConnection.Con))
|
1

Assuming dc is your connection, then it has to be initialized with a connection string. Maybe - if it's a class - you have to set some properties, like database path, etc.

1 Comment

Yes it's a class, I add this class in the code of my question
0
SqlConnection Con= New SQLConnection(@"Data Source=(local)\\SQLEXPRESS; Initial Catalog=MLQ7024; Integrated Security=TRUE"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.