Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gr = GridView1.Rows[e.RowIndex] as GridViewRow;
        TextBox t1 = gr.FindControl("Empid") as TextBox;
        TextBox t2 = gr.FindControl("Name") as TextBox;
        TextBox t3 = gr.FindControl("Emailid") as TextBox;
        
        SqlConnection conn = new SqlConnection(@"Data Source=ARVIND-VAIO\ARVIND;Initial Catalog=ARVIND;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("update Emp_info set Name='"+t2.Text+"',Emailid='"+t3.Text+"' where Empid='"+t1.Text+"'", conn);

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        display();
    }

public void display()
    {
        SqlConnection conn = new SqlConnection(@"Data Source=ARVIND-VAIO\ARVIND;Initial Catalog=ARVIND;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select * from Emp_info", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


    }


whats the error here?its saying Object reference not set to an instance of object in the sql command line..pls rectify the code and help

thanks in advance
Posted
Updated 11-May-13 20:03pm
v4

1 solution

As I told you when I answered this question yesterday:
Two problems here:
1) Either one of your names is wrong: "txtEmpid", "txtName", or "txtEmailid" is incorrect, or one of them is not a textbox. This means that the "tn" variable gets a null, so you get the error when you try to use it's Text property. Check the names and types.
 
2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. This is particularly a problem with website code, where your database can be deleted from the other side of the world...


Please do not re-post the same question. Object reference not set to an instance of an object.[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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