Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a GridView which performs Edit,Update and Delete opearitons. I have Textboxes in EditItemTemplate. The Edit is working properly but the event GridView1_RowUpdating returns zero for executenonquery.

Here is my storedprocedure

SQL
ALTER PROC Reg_UpdateProc    
@COUNTRY VARCHAR(MAX),    
@TITLE VARCHAR(MAX),    
@FIRSTNAME VARCHAR(MAX),    
@LASTNAME VARCHAR(MAX),    
@CITY VARCHAR(MAX),    
@EMPADDRESS VARCHAR(MAX),    
@ZIP VARCHAR(MAX),    
@EMPSTATE VARCHAR(MAX),    
@PHONE VARCHAR(MAX),    
@TOPIC VARCHAR(MAX),    
@COMMENTS VARCHAR(MAX),
@EMAIL VARCHAR(MAX)    
as    
BEGIN    
UPDATE RegisterTable set country=@COUNTRY,Title=@TITLE,firstname=@FIRSTNAME,lastname=@LASTNAME,    
empaddress=@EMPADDRESS,zip=@ZIP,empstate=@EMPSTATE,phone=@PHONE,topic=@TOPIC,    
comments=@COMMENTS WHERE email=@EMAIL    
END

and my Gridview code on GridView1_RowUpdating

C#
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection con = new SqlConnection(Connection);
        con.Open();
        SqlCommand cmd = new SqlCommand("Reg_UpdateProc", con);
        Label country = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
        TextBox txtcountry = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        Label title = (Label)GridView1.Rows[e.RowIndex].FindControl("Label2");
        TextBox txttitle = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        Label firstname = (Label)GridView1.Rows[e.RowIndex].FindControl("Label3");
        TextBox txtfirstname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        Label lastname = (Label)GridView1.Rows[e.RowIndex].FindControl("Label4");
        TextBox txtlastname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
        Label empaddress = (Label)GridView1.Rows[e.RowIndex].FindControl("Label5");
        TextBox txtempaddress = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
        Label city = (Label)GridView1.Rows[e.RowIndex].FindControl("Label6");
        TextBox txtcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
        Label zip = (Label)GridView1.Rows[e.RowIndex].FindControl("Label7");
        TextBox txtzip = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
        Label empstate = (Label)GridView1.Rows[e.RowIndex].FindControl("Label8");
        TextBox txtempstate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8");
        Label phone = (Label)GridView1.Rows[e.RowIndex].FindControl("Label9");
        TextBox txtphone = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9");
        Label email = (Label)GridView1.Rows[e.RowIndex].FindControl("Label10");
        TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox13");
        Label topic = (Label)GridView1.Rows[e.RowIndex].FindControl("Label11");
        TextBox txttopic = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox11");
        Label comments = (Label)GridView1.Rows[e.RowIndex].FindControl("Label12");
        TextBox txtcomments = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox12");
        cmd.CommandType = CommandType.StoredProcedure;   
        cmd.Parameters.AddWithValue("@COUNTRY", txtcountry.ToString());
        cmd.Parameters.AddWithValue("@TITLE", txttitle.ToString());
        cmd.Parameters.AddWithValue("@FIRSTNAME", txtfirstname.ToString());
        cmd.Parameters.AddWithValue("@LASTNAME", txtlastname.ToString());
        cmd.Parameters.AddWithValue("@EMPADDRESS", txtempaddress.ToString());
        cmd.Parameters.AddWithValue("@CITY", txtcity.ToString());
        cmd.Parameters.AddWithValue("@ZIP", txtzip.ToString());
        cmd.Parameters.AddWithValue("@EMPSTATE", txtempstate.ToString());
        cmd.Parameters.AddWithValue("@PHONE", txtphone.ToString());
        cmd.Parameters.AddWithValue("@TOPIC", txttopic.ToString());
        cmd.Parameters.AddWithValue("@COMMENTS", txtcomments.ToString());
        cmd.Parameters.AddWithValue("@EMAIL", txtemail.ToString());
     
        int ret = cmd.ExecuteNonQuery();
        if (ret == 1)
        {
            TextBoxCity.Text = "okay";
        }
        else
        {
            Response.Write("not done");
        }
        con.Close();
    }
}


The result I get is "not done."

Kindly help. Thanks in advance.
Posted
Updated 23-Dec-13 17:41pm
v2
Comments
Aydin Homay 23-Dec-13 23:47pm    
Please add more information about you error same as error message and lab lab lab
Karthik_Mahalingam 23-Dec-13 23:58pm    
what error u r getting??
Krishna Siva 24-Dec-13 0:01am    
I get no error guys. But the record is not updated :(

1 solution

Well, there's so much wrong here, and so much to go wrong here.

Your code is awful. You have not named your controls logically. How can you be sure you got it right and that textbox13 is the email and not the phone number ? This code is unreadable and unmaintainable.

You should not be creating connections in the presentation layer, you should use a data layer and ONE method should return connections. You should read up on how connection pools work for the versions you're using, and work with it. You should certainly use using blocks for anything that has IDisposable.

You should be matching on something other than an email address. Does your DB enforce an email only existing in one row ?

Do you know how to use your debugger ? Are you certain the data is not being updated ? What if you get the values you're passing in to the proc, and call the proc in SQL Server ? Have you checked at all what the email value is, going in ? Odds are that it's not matching ( leading spaces, perhaps ? ).

*sigh* did you write this code at random ? So much is broken here, it took me a while to spot the issue.

C#
cmd.Parameters.AddWithValue("@COUNTRY", txtcountry.ToString());
      cmd.Parameters.AddWithValue("@TITLE", txttitle.ToString());
      cmd.Parameters.AddWithValue("@FIRSTNAME", txtfirstname.ToString());
      cmd.Parameters.AddWithValue("@LASTNAME", txtlastname.ToString());
      cmd.Parameters.AddWithValue("@EMPADDRESS", txtempaddress.ToString());
      cmd.Parameters.AddWithValue("@CITY", txtcity.ToString());
      cmd.Parameters.AddWithValue("@ZIP", txtzip.ToString());
      cmd.Parameters.AddWithValue("@EMPSTATE", txtempstate.ToString());
      cmd.Parameters.AddWithValue("@PHONE", txtphone.ToString());
      cmd.Parameters.AddWithValue("@TOPIC", txttopic.ToString());
      cmd.Parameters.AddWithValue("@COMMENTS", txtcomments.ToString());
      cmd.Parameters.AddWithValue("@EMAIL", txtemail.ToString());


ToString will return the name of the control. The Text property will return the Text in the control. Learn to use a debugger, and read books to learn how to write code, instead of just playing with the autocomplete and grabbing things at random.
 
Share this answer
 
Comments
Nandakishore G N 24-Dec-13 0:24am    
Super answer..Graus..My 5..
Krishna Siva 24-Dec-13 5:34am    
Sorry Graus..Do not get me wrong. Yes. This is a sample I have created and not a real-world code. I am working on GridViews. Thanks for your advice. Since I am new to this community, hereafter I will review my code before posting it..

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