Alen,
I suppose you are using webforms.
1. Command's connection
The object SqlCommand needs a SqlConnection to execute the operation.
Something like this:
SqlConnection objConnection = new SqlConnection("your connection string
here");
SqlCommand cmd = new SqlCommand("select * from mid_zone where p_id=2",
objConnection);
2. ExecuteNonQuery is not the correct method
ExecuteNonQuery returns an integer representing the number of rows affected,
it's not what you want.
You want to read the values coming from the select, so you should use
ExecuteReader. Like this:
IDataReader rd = cmd.ExecuteReader();
3. Getting the data of the DataReader
In order to retrieve the field's value, you need to access it by its name:
rd["FieldName"]
4. Binding values to the labels
I don't know exactly your scenario, but you have some options:
You can bind it dinamically, or you can hardcode it:
Hardcoding it:
lblMyLabelId.Text = rd["FieldName"].ToString();
Dinamically:
foreach (Control objControl in yourFormOrContainer.Controls)
{
if(objControl != null && objControl.GetType() == typeof(Label))
{
// It means that the control is a label
((Label) objControl).Text = rd["FieldName"].ToString();
}
}
Hope it helps.
Best regards,
Guilherme Utrabo
On 3 May 2010 08:42, Alen Alexander <[email protected]> wrote:
> Frnds,
> My Web page contains multiple Label controls(10 in total). I want to fill
> all those controls with the values extracted from database.
> But corresponding labels are not getting filled with Right data from
> database.
> I am using ASP.NET with C# and SQL Server 2005.
> My Code for getting values are..
>
> cmd = new SqlCommand("select * from mid_zone where p_id=2");
> rd= cms.ExecuteNonQuery();
> while(rd.Read())
> {
> //reading values
> }
> Please help...
> Thanks in Advance
> --
>
>