Thanks for your inputs... but my problem wont get solved completely.
My Database Design is as follows--
Table Name:-Page
Fields:-
sectionid int
header varcher
description varchar
image varchar
So, in each Section.... i have Header,Description and a image control.
Header and desc are labels.
I need to fill each label based on SectionID.
ie Sectionid 1's data(header,desc,image) should go into correct Labels and
so on.
I have about 6 Sections.ie 6X2 Labels.
Again Thanks for your reply.
On Mon, May 3, 2010 at 8:35 PM, Guilherme Utrabo <[email protected]> wrote:
> 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
>> --
>>
>>
>
--
Alen
Some people have hundreds of reason why they cannot do what they want to,
when all they need is one reason why they can!!!!