Error when i try to run Update query

Joined
Apr 7, 2011
Messages
1
Reaction score
0
Hi, I want to update my table when I run the query. Everything works fine, only Update query doesn't work here, I can't execute them.
I get error on the bold line... Does anyone know, how I can fix this problem ?
The error is:
Line 58: SqlCommand cmdUpdate = new SqlCommand("UPDATE tblProbleemOmschrijving SET Status='A' WHERE ID= @ID");
Line 59: cmdUpdate.Parameters.AddWithValue("@ID",ID_ProbleemOmschrijving);
Line 60: cmdUpdate.ExecuteNonQuery();

ExecuteNonQuery: Connection property has not been initialized.

Code:
try
        {
            // Get the currently selected row using the SelectedRow property.
            GridViewRow row = GridView1.SelectedRow;

            string ID_ProbleemOmschrijving = row.Cells[1].Text;
            string afdeling = row.Cells[3].Text;
            string probleem = row.Cells[4].Text;
            string datum = txtDatum.Text;
            string tijd = txtTijd.Text;
            string omschrijving = txtOmschrijving.Text;
            //----test---
            DateTime date = Convert.ToDateTime(txtDatum.Text);

            //Connectie DB
            string conStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\NaskaIngredients.mdf;Integrated Security=True;User Instance=True;";

            //SQLConnection
            SqlConnection myConn = new SqlConnection(conStr);

            myConn.Open();

            SqlCommand cmdSelect = new SqlCommand("insert into tblProbleemReparatie (ID_ProbleemOmschrijving, Afdeling, Soort_probleem, Omschrijving_reparatie, Uur_reparatie, Date_reparatie) VALUES(@ID_ProbleemOmschrijving, @Afdeling, @Soort_probleem, @Omschrijving_reparatie, @Uur_reparatie, @Date_reparatie)", myConn);
           
            cmdSelect.Parameters.AddWithValue("@ID_ProbleemOmschrijving", ID_ProbleemOmschrijving);
            cmdSelect.Parameters.AddWithValue("@Afdeling", afdeling);
            cmdSelect.Parameters.AddWithValue("@Soort_probleem", probleem);
            cmdSelect.Parameters.AddWithValue("@Omschrijving_reparatie", omschrijving);
            cmdSelect.Parameters.AddWithValue("@Uur_reparatie", tijd);
            cmdSelect.Parameters.AddWithValue("@Date_reparatie", date);
            cmdSelect.ExecuteNonQuery();
            
            [B]SqlCommand cmdUpdate = new SqlCommand("UPDATE tblProbleemOmschrijving SET Status='A' WHERE ID="+ID_ProbleemOmschrijving);
            cmdUpdate.ExecuteNonQuery();[/B]

            // 3) SQLDataReader
            myConn.Close();

            GridView1.DeleteRow(GridView1.SelectedIndex);
            
            txtDatum.Text = "";
            txtTijd.Text = "";
            txtOmschrijving.Text = "";
            imgGoedkeuring.Visible = true;
            imgFout.Visible = false;
            lblFout.Visible = false;
            lblSucces.Text = "De gegevens zijn succesvol opgeslagen in de database";

            txtDatum.Enabled = false;
            txtTijd.Enabled = false;
            txtOmschrijving.Enabled = false;
        }
        catch
        {
            imgFout.Visible = true;
            lblFout.Text = "ERROR";
        }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top