将SQL数据值填充到asp.net中的文本框? [英] Populate sql data value to text box in asp .net?
本文介绍了将SQL数据值填充到asp.net中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为txtItemID的文本框。使用名为Item的Sql数据库表。它有一个名为ItemID的列,它是使用max(ItemID)+1值创建的。我的要求是每当页面加载发生时(在asp .net中运行我的页面),max(ItemID)+1值应显示在文本框txtItemID中。我已经完成了这些代码,但它不起作用:
Hi,
I have a text box named txtItemID. An Sql data base table named Item is used. It has the column named ItemID, which is created by using max(ItemID)+1 value. My requirement is whenever the page load occurs(run my page in asp .net), the max(ItemID)+1 value should be shown in the text box txtItemID. I have done these codes but it doesn't works :
private void BindItemID()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Select ItemID from Item", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
txtItemID.Text = dt.ToString();
txtCode.DataBind();
}
推荐答案
将从表'Item'返回最后一个ItemID
Will return the last ItemID from table 'Item'
SELECT IDENT_CURRENT('Item')
将值填充到文本框
Will populate the value to textbox
txtItemID.Text = Convert.ToString(dt.Rows[0]["ItemID"]);
使用类似
Hi,
Use like
private void BindItemID()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);
string sItemID = string.Empty;
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Select max(ItemID)+1 from Item", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
sItemID = Convert.ToString(cmd.ExecuteScalar());
txtItemID.Text = sItemID.ToString().Trim();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
这篇关于将SQL数据值填充到asp.net中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文