Solved

Displaying a field name from table into  a label

Posted on 2007-11-21
13
225 Views
Last Modified: 2013-12-16
Hi,
I am trying to get a value into a label from the database table.
But after i created a sqlcommand object i am not able to figure out how to get that value into the label.
I am using visual studio 2005 and C#.

Thank you.
0
Comment
Question by:srini4595
13 Comments
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 20328134
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20328197
SqlConnection connection = new SqlConnection("ConnectionString");
SqlCommand comm = new SqlCommand (statement,connection);
connection.open();
SqlDataReader  reader = comm.ExecuteReader();
DataTable tbl = new DataTable();
tbl.load(reader);
connection.close();

label.text = tbl.column[i].Name;
0
 

Author Comment

by:srini4595
ID: 20328542
Thank you messen. I think your solution would help me out.
I tried that, I am not getting error but the field is not displayed from the table. I am a beginner to this so please help me out in this.
The code I wrote is :
                                 SqlCommand cmd1;
                                 cmd1 = new SqlCommand("Select ServerName from ConnectionTable where  FieldName='Name' And Active='True'",con);
                                 con.Open();
                                 if (ddlName.Text == "ABNC")
                                    {
                                       SqlDataReader reader = cmd1.ExecuteReader();
                                       DataTable tbl = new DataTable();
                                       tbl.Load(reader);
                                       lblDatabaseUsed.Text = tbl.Columns["ServerName"].ToString();
                                 }
           

Thank you.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Expert Comment

by:messen1975
ID: 20328566
Try

 lblDatabaseUsed.Text = tbl.Columns["ServerName"].ColumnName.ToString();

0
 
LVL 6

Expert Comment

by:messen1975
ID: 20328573
Although the "ToString()" method is not really required because the ColumnName property is a string.
0
 

Author Comment

by:srini4595
ID: 20328677
It is displaying the text ServerName in the Label instead of the value od ServerName from the database.
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20328686
Perhaps I'm misunderstanding -- I thought you wanted the name of the column in the label, not a value IN the column in a row?
0
 

Author Comment

by:srini4595
ID: 20328701
Ho, I am sorry for that, I want the value of that field from the database in the label.
0
 
LVL 6

Accepted Solution

by:
messen1975 earned 250 total points
ID: 20328732
Ok --

if (tbl.Rows.Count = 1)
{
    lblDatabaseUsed.Text = tbl.rows[0,"ServerName"].ToString();
}
else
{
   Foreach(DataRow row in tbl.rows)
{
   If(row["ServerName"].equals("SomeValue")
  {
    lblDatabaseUsed.Text =row["ServerName"].ToString();
  }
}
}
0
 

Author Comment

by:srini4595
ID: 20328953
It says : No overload for method 'this' takes '2' arguments.
At  
      lblDatabaseUsed.Text = tbl.Rows[0, "ServerName"].ToString();
0
 
LVL 10

Expert Comment

by:joex911
ID: 20329795
Hi, try this instead:

lblDatabaseUsed.Text = tbl.Rows[0][ "ServerName"].ToString();
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20329930
joe is right it should be

lblDatabaseUsed.Text = tbl.Rows[0][ "ServerName"].ToString();
0
 

Author Comment

by:srini4595
ID: 20330276
Thank you very much Joe.
messen thank you for your effort in helping me out.
Thank you.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question