简体   繁体   中英

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".

How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net.

I mean what should be the code if I want label text to be "fname" value that is "Nikhil"

Connection is already done properly because I am able to display table data in Gridview but not able to display it in label.

I had also seached but not understood the answer

label1.Text = ?; // I want fname here

Regards,

Nikhil

If you have a data in a table dtData then you can use this code. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM