Error when i try to run Update query

Discussion in '.NET' started by cengizb, Apr 7, 2011.

  1. cengizb

    cengizb

    Joined:
    Apr 7, 2011
    Messages:
    1
    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";
            }
     
    cengizb, Apr 7, 2011
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bienwell
    Replies:
    4
    Views:
    3,854
    bienwell
    May 27, 2005
  2. John Salerno
    Replies:
    20
    Views:
    859
    John Salerno
    Aug 11, 2006
  3. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    378
  4. =?Utf-8?B?QUo=?=

    When to TRY and not to TRY

    =?Utf-8?B?QUo=?=, Oct 16, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    330
    sloan
    Oct 17, 2007
  5. MRAB
    Replies:
    4
    Views:
    311
Loading...

Share This Page