Solved

Displaying a field name from table into  a label

Posted on 2007-11-21
13
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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