从一个按钮点击火灾插入和更新存储过程 [英] Fire Insert and Update Stored procedure from One Button Click

查看:100
本文介绍了从一个按钮点击火灾插入和更新存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个Asp.net表单,因为我有Submit按钮,将文本框数据插入数据库并绑定gridview。在网格视图中,我有一个链接按钮,用于填充该网格视图的选定行的文本框。现在在同一个提交按钮上我想触发更新存储过程以更新所选记录,请帮助我如何完成相同的操作。这是我的代码



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

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();
                
            }
        }

推荐答案

据我所知,你正在填充 TextBox 单击 LinkBut​​ton 时。



因此,您必须绑定 GridView 主键值>。

使用 GridView DataKey 属性进行绑定。



现在,当 LinkBut​​ton DataKey 单击该行的c>。

执行相同的存储过程,检查是否有 Priamry Key ID 的记录( DataKey )。



如果存在,则更新行,否则插入行。
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.


因为我理解你的问题。您希望使用相同的提交按钮来插入和更新任务。为此,您需要有一个隐藏字段,通过单击提交按钮说明您现在要执行的操作。比如在隐藏字段中,您可以根据您的要求提供行号或其他内容。基于此,您可以立即管理您在提交按钮点击时必须执行的操作。



但我建议为这两项任务设置不同的按钮。



如果有帮助则标记为答案。



问候,

Dheeraj Gupta
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


您可以尝试如下:



1.第一次插入文本框值时,您没有任何主键与该交易相关联。所以你可以去插入SP。



2.但是当你更新记录时,你可以轻松检查该记录的主要密钥值,如果它是然后你可以决定去更新存储过程。



我希望这会对你有所帮助。
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.


这篇关于从一个按钮点击火灾插入和更新存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆