插入两个程序 [英] Insert two procedure

查看:82
本文介绍了插入两个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的页面有问题.我有2个程序.第一个是从sql server中选择数据,第二个是如果更改了行则更新数据.

当我在C#上运行该过程时,它们可以工作,但是当我同时运行这两个过程时,则不会更新数据.无法识别我更改了数据.我的代码:

Hi,
I have a problem with my page. I have 2 procedure. First one is to select data from sql server and the second is to update the data if the row is change.

When I am running the procedure on C# they work, but when I run the both procedure it does not update the data. It does not recognize that I changed the data. My code:

using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data.SqlTypes; 
public partial class controll_update : System.Web.UI.Page 
{ 
SqlConnection con; 
SqlCommand cmd = new SqlCommand(); 
SqlDataReader rd = null; 
protected void Page_Load(object sender, EventArgs e) 
{ 
TextBoxid1.Text = Page.Request.QueryString["id"]; 
con = new SqlConnection("Server=localhost;Initial Catalog=shahar;Integrated Security=True;"); 
cmd = new SqlCommand("select_for_update", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.Add("@id", SqlDbType.NChar, 20).Value = Page.Request.QueryString["id"]; 
con.Open(); 
rd = cmd.ExecuteReader(); 
if (rd.Read()) 
TextBoxfname1.Text = rd[1].ToString(); 
TextBoxid2.Text = rd[3].ToString(); 
TextBoxlname.Text = rd[2].ToString(); 
con.Close(); 

} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
TextBoxid1.Text = Page.Request.QueryString["id"]; 
con = new SqlConnection("Server=localhost;Initial Catalog=shahar;Integrated Security=True;"); 
cmd = new SqlCommand("update_pro", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.Add("@id", SqlDbType.NChar).Value = Page.Request.QueryString["id"]; 
cmd.Parameters.Add("@fname", SqlDbType.NChar).Value = TextBoxfname1.Text; 
cmd.Parameters.Add("@lname", SqlDbType.NChar).Value = TextBoxlname.Text; 
cmd.Parameters.Add("@id_num", SqlDbType.NChar).Value = TextBoxid2.Text; 
con.Open(); 
cmd.ExecuteNonQuery(); 
con.Close(); 
} 

protected void TextBoxfname1_TextChanged(object sender, EventArgs e) 
{ 

} 
protected void TextBoxlname_TextChanged(object sender, EventArgs e) 
{ 

} 
} 


我没有任何错误,它只是返回原始数据.

谢谢


I dont have any error, it just goes back to original data.

Thank you

推荐答案

它的行为与您所告诉的完全相同.首次访问该页面时,PgaeLoad事件将填充数据库中的文本框.输入新文本并单击按钮后,将生成回发,这会导致PageLoad事件再次触发,从而使文本框返回数据库中的原始值.

使用IsPostBack

It is behaving exactly as you are telling it to. On the initial visit to the page the PgaeLoad event populates the textboxes from the database. After entering new text and clicking the button a postback is generated which causes the PageLoad event to fire once again, returning the textboxes to the original values from the database.

use IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    // existing code
  }

}


哇,太简单了...非常感谢,它对您有帮助!
wow, it was so simple... thank you very much, it helped!!!


这篇关于插入两个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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