• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Displaying a field name from table into a label

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
srini4595
Asked:
srini4595
1 Solution
 
SQL_SERVER_DBACommented:
0
 
messen1975Commented:
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
 
srini4595Author Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
messen1975Commented:
Try

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

0
 
messen1975Commented:
Although the "ToString()" method is not really required because the ColumnName property is a string.
0
 
srini4595Author Commented:
It is displaying the text ServerName in the Label instead of the value od ServerName from the database.
0
 
messen1975Commented:
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
 
srini4595Author Commented:
Ho, I am sorry for that, I want the value of that field from the database in the label.
0
 
messen1975Commented:
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
 
srini4595Author Commented:
It says : No overload for method 'this' takes '2' arguments.
At  
      lblDatabaseUsed.Text = tbl.Rows[0, "ServerName"].ToString();
0
 
Oliver AmayaEntrepeneurCommented:
Hi, try this instead:

lblDatabaseUsed.Text = tbl.Rows[0][ "ServerName"].ToString();
0
 
messen1975Commented:
joe is right it should be

lblDatabaseUsed.Text = tbl.Rows[0][ "ServerName"].ToString();
0
 
srini4595Author Commented:
Thank you very much Joe.
messen thank you for your effort in helping me out.
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now