Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Displaying a field name from table into  a label

Posted on 2007-11-21
13
Medium Priority
?
231 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

886 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