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?
Loading
Santhosh Kumar JayaramanPosted Jun 29, 2012, 3:52 AM
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();cmd.connection=conn;
cmd.CommandText = "INSERT INTO tabelkuliah (Day, Subject, Time, Lecture, Location)" + " VALUES (texBox1.text, texBox2.text, texBox3.text, texBox4.text, texBox5.text)";
conn.ConnectionString ="Data Source=.\SQLExpress;" +
"User Instance=true;" +
"User Id=UserName;" +
"Password=Secret;" +
"AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();
int result = cmd.ExecuteNonQuery();
try this..