[英]How to retrieve column value of sql server 2005 table and store it in label.Text of c# ASP.Net
My question is 我的问题是
Suppose I have a Column "fname" whose value is 'Nikhil' in table "profile". 假设我在“配置文件”表中有一列“ fname”,其值为“ Nikhil”。
How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net. 如何检索SQL Server表的列值并将其存储在C#ASP.Net的label.Text中。
I mean what should be the code if I want label text to be "fname" value that is "Nikhil" 我的意思是,如果我希望标签文本为“ Nikhil”的“ fname”值,应该是什么代码
Connection is already done properly because I am able to display table data in Gridview but not able to display it in label. 连接已经正确完成,因为我可以在Gridview中显示表数据,但不能在标签中显示表数据。
I had also seached but not understood the answer 我也一直在寻找,但不知道答案
label1.Text = ?; label1.Text =?; // I want fname here //我想在这里使用fname
Regards, 问候,
Nikhil 尼基尔
If you have a data in a table dtData then you can use this code. 如果表dtData中有数据,则可以使用此代码。 int rowindex = 0;
Label1.Text=dtData.Rows[rowindex]["column name"].ToString();
and from grid view use this code Label1.Text = GridView1.Rows[0].Cells[0].Text
Cell is the column in the grid view 并从网格视图使用此代码Label1.Text = GridView1.Rows[0].Cells[0].Text
单元格是网格视图中的列
Note that Your query needs to be whatever it is to get the proper name 请注意,您的查询必须是正确的名称
String fname = ""
// Create a new SqlCommand with your query to get the proper name and the connection string
SqlCommand getFnameCommand = New SqlCommand("SELECT TOP 1 fname FROM profile ORDER BY fname DESC;", New SqlClient.SqlConnection"Your Connection String"))
getFnameCommand.Connection.Open() // Open the SqlConnection
fname = getFnameCommand.ExecuteScalar() // This pulls the result from the first column of the first row of your query result
getFnameCommand.Connection.Close() //Close the SqlConnection
label1.text=fname; //Set your label text to the data you just pulled
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.