radit

radit

  • NA
  • 1
  • 1.6k

How to resolve exception like this "connection has not been initialized"

Jun 29 2012 3:13 AM
im using c#.net 2010 and i'm working with database using microsoft sql express, i have create a table and connect it to my form but i cant insert record to the table. i always got an exception like this "connection has not been initialized". here are my codes


{

con.ConnectionString =
cmd.Connection = con;
cmd =
cmd.CommandText =
private void button1_Click(object sender, EventArgs e)SqlConnection con = new SqlConnection();SqlCommand cmd = new SqlCommand();"Data Source=.\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + "Integrated Security=True;User Instance=True";new SqlCommand();"INSERT INTO tabelkuliah (Day, Subject, Time, Lecture, Location)" + " VALUES (texBox1.text, texBox2.text, texBox3.text, texBox4.text, texBox5.text)";try{
con.Open();
{
}
int result = cmd.ExecuteNonQuery();if (result > 0)MessageBox.Show("Succeed!");else{
}
con.Close();
}
{

}
}
MessageBox.Show("Failed!");MessageBox.Show("ExecuteNonQuery Success!");catch (Exception ex)MessageBox.Show("Can't ExecuteNonQuery! " + ex);

do you have any idea to fix it?

Answers (1)