Solved

Displaying a field name from table into  a label

Posted on 2007-11-21
13
226 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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:Oliver Amaya
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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