Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.
how can i fill labels or textboxes from a select query?
for examle my select reture the four fields ( name , brand , model , price)
i want to do that in asp.net
thanks all
Posted
Comments
[no name] 2-Aug-15 11:29am    
You set the text property of the controls. Do you have an actual question?

Instead of writing the data yourself. I would recommend that you use DataGrid controls. Just pass the result of the query to the DataGrid and it would take care of it itself. DataGrid uses the data you pass, and iterates over the data and fills the table. It creates the column and rows itself.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid(v=vs.110).aspx[^]
https://msdn.microsoft.com/en-us/library/aa479316.aspx[^]

If you do not use the control, you would have to manually implement the algorithm. To generate a table and write header rows using the column names and for each record loop the other rows and fill up the table. It would take a lot of time.
 
Share this answer
 
v2
Comments
Member 11829564 2-Aug-15 12:41pm    
yeah, i don't want use any control!
thanks for your time
if you searching and display single item from database you can do as below
C#
using (var connection = new SqlConnection("connection string"))
{
    connection.Open();
    using (var cmd = new SqlCommand("SELECT name , brand , model , price  FROM Product WHERE ProductID=@MYVALUE", connection))
    {
        cmd.Parameters.AddWithValue("@MYVALUE", txtID.Text);
        SqlDataReader re = cmd.ExecuteReader();

        if (re.Read())
        {
            txtName.Text = (string)re["name"]; 
            txtBrand.Text = (string)re["brand"];
            txtModel.Text = (string)re["model"];
            txtPrice.Text = (string)re["price"];
        }
        else
        {
            MessageBox.Show("Please enter a valid product id");
        }
    }
} 

based on user input, sql command will fetch the data and it will display the record details on UI labels.

if you need to show multiple records, you better use controls like DataGridView or ListView.
 
Share this answer
 
Comments
Member 11829564 2-Aug-15 12:41pm    
thanks man

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900