Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hello friends , I have got an Asp.net form , in that I have Submit button that inserts the textbox data to the data base and binds the gridview . In the grid view I have a link button that fills the text box for the selected row of that gridview. Now on the same submit button I want to fire Update Stored Procedure to update the selected records , Please Help me How can I accomplish the same. Here is my code

C#
protected void btnSubmit_Click(object sender, EventArgs e)
        {
        try
        {
            con2.Open();
            cmd = new SqlCommand("USP_INSERT_STUDENT_NOTICE",con2);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@NOTICEDISPLAY_DATE", txtnoticedate.Text);
            cmd.Parameters.AddWithValue("@NOTICE_TITLE", txtnoticetitle.Text);
            cmd.Parameters.AddWithValue("@NOTICE_BODY", txtnoticebody.Text);
            int rows = cmd.ExecuteNonQuery();
            if (rows>0)
            {
                Response.Write("<script>alert('Data Inserted Succesfully');</script>");
                GridView1.DataBind();
                Response.Redirect("~/Admin/AddStudentNotice.aspx");
              //  
                

                con2.Close();
                
            }
        }
Posted
Updated 30-Jul-13 0:23am
v3
Comments
[no name] 30-Jul-13 6:18am    
what is the error u are getting..

As far as I understand, you are populating the TextBox when LinkButton is clicked.

So, you must be binding the Primary Key value of each row in GridView.
Else bind that by using the DataKey property of GridView.

Now, try to get the DataKey of the Row, when LinkButton of that Row is clicked.
Execute the same Stored procedure, check if there is a record for that Priamry Key ID (DataKey) in the Table.

If it exists, then update the row, else insert the row.
 
Share this answer
 
Comments
Kunal Ved 30-Jul-13 6:40am    
Thnks for your reply can you give an example for the same
Check here to know how to use DataKey - GridView.DataKeys Property.

On Button Click, get the DataKey value of Selected row and pass that as a parameter to the Stored Proc and check as I said...
[no name] 30-Jul-13 6:44am    
+5 Tadit..
Thanks a lot Code-Hunt... :)
Nandakishore G N 30-Jul-13 8:20am    
good answer..+5 Tadit..
As i am understanding your question. you want to use same submit button for both tasks insert and update. For this you need to have an hidden field which will say that what operation you are now going to perform by clicking on submit button. like in hidden field you can provide the row no or something else as per your requirement. based on which you can manage now what you have to perform on submit button's click.

But I suggest to have different buttons for both tasks.

Mark as Answer if it helps.

Regards,
Dheeraj Gupta
 
Share this answer
 
You may try like below :

1. When you insert a text box value for the first time, you don't have any primary key associated with that transaction.So you can go for insert SP.

2. But When you update the record,you can easily check that record's primary key vale and If it's there then you can decide to go for update stored procedure.

I hope this will help to you.
 
Share this answer
 
Comments
Kunal Ved 30-Jul-13 6:50am    
Please Explain with an example . It would be helpful
Sampath Lokuge 30-Jul-13 7:12am    
I hope you can follow what @Tadit said on his solution.my solution is some what equal for his one.

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