如何使用存储过程在文本框中插入数据 [英] How To Insert Data in Textboxes using Store Procedure
本文介绍了如何使用存储过程在文本框中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一些使用存储过程在aspx页面中建立的文本框中插入一些数据.
我在存储过程中将select查询写为:
I want to insert some data in my textboxes that I have built in my aspx page using store procedure.
I have written select query in store procedure as:
ALTER PROCEDURE [dbo].[GetUserDetails]
@username VARCHAR(20),
@empname VARCHAR(20),
@email VARCHAR(30),
@designation VARCHAR(20),
@reportto VARCHAR(20),
@r int output,
@Result VARCHAR(50) output
AS
BEGIN
SELECT @r = COUNT(*) FROM Users WHERE username = @username
SELECT @Result = username FROM Users WHERE username = @username
END
在我的aspx页面中,我希望当用户从下拉列表中选择日期时,应根据存储过程将数据插入文本框中.
程序运行成功,调试时我也在cmd参数中获取用户名,但未在文本框中获取数据.
我的aspx.cs代码类似于:
In my aspx page I want that when user select days from dropdown list data should be inserted in textboxes according to store procedure.
Programme is running successfully and i am getting username in cmd parameters also while debugging but not getting data in textboxes.
My aspx.cs code is like:
protected void Page_Load(object sender, EventArgs e)
{
username = Convert.ToString(Request.QueryString["username"]);
}
protected void drpDays_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("GetUserDetails", sqlcon);
cmd.Connection = sqlcon;
cmd.CommandText = "GetUserDetails";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p0,p1, p2, p3,p4,p5,p6;
p0 = cmd.Parameters.Add("@username", SqlDbType.VarChar, 20);
p0.Value = username;
p1 = cmd.Parameters.Add("@empname", SqlDbType.VarChar,20);
p1.Direction = ParameterDirection.Input;
p1.Value = txtName.Text;
p2 = cmd.Parameters.Add("@email", SqlDbType.VarChar, 30);
p2.Direction = ParameterDirection.Input;
p2.Value = txtEmail.Text;
p3 = cmd.Parameters.Add("@designation", SqlDbType.VarChar, 20);
p3.Direction = ParameterDirection.Input;
p3.Value = txtDesignation.Text;
p4 = cmd.Parameters.Add("@reportto", SqlDbType.VarChar, 20);
p4.Direction = ParameterDirection.Input;
p4.Value = txtReportTo.Text;
p5 = cmd.Parameters.Add("@Det", SqlDbType.Int);
p5.Direction = ParameterDirection.Output;
p6 = cmd.Parameters.Add("@Result", SqlDbType.VarChar, 50);
p6.Direction = ParameterDirection.Output;
txtFrom.Enabled = true;
txtTo.Enabled = true;
sqlcon.Open();
cmd.ExecuteNonQuery();
int x = int.Parse(cmd.Parameters["@r"].Value.ToString());
string result = cmd.Parameters["@Result"].Value.ToString();
if (x == 1)
{
int SelectDays = Convert.ToInt32(drpDays.SelectedValue);
for (int i = 0; i < SelectDays; i++)
{
TableRow tr = new TableRow();
//Create Column 1
TableCell td1 = new TableCell();
//Cretae a Label Control Dynamically
TextBox TextBox1 = new TextBox();
TextBox1.ID = "txtDate" + i.ToString();
td1.Controls.Add(TextBox1);
//Create Column 2
TableCell td2 = new TableCell();
TextBox TextBox2 = new TextBox();
TextBox2.ID = "txtDescription" + i.ToString();
TextBox2.TextMode = TextBoxMode.MultiLine;
//Add Control to the Table cell
td2.Controls.Add(TextBox2);
//Add cell to the Row
tr.Cells.Add(td1);
tr.Cells.Add(td2);
//Add row to the table
tblDynamic.Rows.Add(tr);
}
}
else
{
lblmsg.Text = "Fail!!";
}
}
推荐答案
fwr,
我要了解的是,您想使用存储过程从表中获取一些数据并将其显示在文本框中.如果我是正确的,那么您必须执行以下操作:
而不是使用"cmd.ExecuteNonQuery();
您必须将datatable和dataadapter用作:
DataTable dt = new DataTable();
DataAdapter da =新的DataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
conn.close();
--------------------------------
现在,您已经在dt中存储了数据.
您可以从中将值填充到文本框,例如:
textbox1.text = dt.rows [0] ["ColumnName1"].ToString(); //ColumnName1是要在db中显示的字段名称.
希望能解决您的问题.
阿努拉格
@cheers @
Hi fwr,
What I got to understand is that you want to fetch some data from the table using your stored procedure and display it in your textboxes. If I am correct, then you must do something like this :
Instead of using "cmd.ExecuteNonQuery();
you must take a datatable and dataadapter as :
DataTable dt = new DataTable();
DataAdapter da= new DataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
conn.close();
--------------------------------
Now , you have got data in your dt.
from which you can fill values to textboxes, like as :
textbox1.text=dt.rows[0]["ColumnName1"].ToString(); //ColumnName1 is the name of your field in db, which you want to display.
Hope, this would solve your problem.
Anurag
@cheers@
那是我见过的最奇怪的事情.为什么将所有数据传递到此存储的proc中?你为什么要进行选择计数(*)?我建议您阅读一本基本的SQL书籍.请求所需的数据,仅传递所需的数据,这样就可以了.
That''s the most bizarre thing I''ve ever seen. Why do you pass all that data in to this stored proc ? Why are you doing a select count(*) ? I suggest you read a basic SQL book. Request the data you want, only pass in the data you need, and you''ll be fine.
这篇关于如何使用存储过程在文本框中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文