Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am doing update using grid view.when i update error occurs as follows;

my code as follows;

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string Sno = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtSno")).Text;
        string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtName")).Text;
        string Age = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtage")).Text;
        string Address = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress")).Text;

        SqlConnection con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Details set Sno=@Sno, Name=@Name," +
     "Age=@Age where Sno=@Sno;" +
     "select Sno,Name,Age,Address from Details";
        cmd.Parameters.Add("@Sno", SqlDbType.Int).Value = Sno;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
        cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = Age;
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
        cmd.ExecuteNonQuery();
        SqlCommand cmd1 = new SqlCommand("Select * from Details", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd1);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        GridView1.EditIndex = -1;
        cmd.Connection = con;
        con.Close();
    }

the error occurs as follows, ExecuteNonQuery: Connection property has not been initialized. ( cmd.ExecuteNonQuery();) in this line error occurs please help me.what is my problem in the above code.
Posted

1 solution

Write this line
C#
cmd.Connection=con;
after
C#
con.Open();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900