Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Puplating Labels

Status
Not open for further replies.

campbelt

Programmer
Dec 8, 2007
5
US
Hello Everyone,

I am new to ASP.NET, but as a bit of background, I come to it with a couples worth of experience with PHP and MySQL, as well as about a year worth of experience with Visual Basic and 6 months experience with C#.

Currently, I am just tinkering around with Microsoft Visual Web Developer 2005 Express Edition, and I am having some trouble that I'm hoping someone here can help me work through.

As my first question, can anyone here help me work out a way to dynamically populate the Text property of a Label control from a value taken from an SQL database?

Thank you in advance,

Troy
 
If you create a connection to the SQL Database (DataAdapter class) and get your results, you can drill into the data row object and assign the result to the Label.Text property.

A very limited example would be something like this(from memory, so verify first):

Code:
SqlDataReader myDataReader = null;

SqlConnection mySqlConnection = new SqlConnection("serverserver;Trusted_Connection=yes;database=northwind");

SqlCommand mySqlCommand = new SqlCommand("SELECT LABELTEXT FROM Labels WHERE ID = 4", mySqlConnection);

mySqlConnection.Open();

myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);

if (myDataRead.Read)
{
Label1.Text = myDataRead.GetString(0);
}

[code]
 
bah...just re-read my response...here's some errata

In the connection string, change "serverserver" to "server=server".

In the conditional test, change "myDataRead.Read" to "myDataReader.Read".

In the Label1.Text assignment, change "myDataRead.GetString(0)" to "myDataReader.GetString(0)".

I think that's it :)
 
macleod1021,

Thank you for replying!

While I was waiting for a resonse to this, I was trying to work it out on my own through research. I did manage to get it working, but I'm not clear on HOW I did. So, perhaps you could help clarify a few things for me?

First, it looks like there are two different ways that the data can be retrieved: DataSet and DataReader. I think your example is of the DataReader type. The solution I managed to find while waiting for this reply is of the DataSet type. But, I don't really know what the difference is, or why someone would choose one over the other. Do you now what the difference is?

For reference, here is the code for the solution I manged to put together. This example uses SqlDataSource1 in order to get access to a PageHeader field in a table, and then prints out that value in to a label control called HeaderLabel:

Code:
Dim dvSql As DataView = DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
For Each drvSql As DataRowView In dvSql
    HeaderLabel.Text = drvSql("PageHeader").ToString()
Next

I pretty much understand what's happening here, but I do not fully understand this portion:

Code:
SqlDataSource1.Select(DataSourceSelectArguments.Empty)

I think the Select method is somehow acting like a SQL SELECT statement, but I'm not sure I understand how, or why the parameter for this function has an .Empty property attached to it. Do you know what all of this means?

I know that this is probably asking a lot, but if you or anyone else could answere these questions, I would really really appreciate it.

In the mean time, I am going to tinker around with the example you gave and see what I can learn from it.

Thank you again,

Troy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top