Click here to Skip to main content
13,297,547 members (50,311 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to select from sql database on page load, and i want to write out that selection to a label.

here is what i have so far, but it seems to not be working.

Session("UserInfo") = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().Username
WelcomeLabel.Text = Session.Item("UserInfo").ToString()
Dim dt As New DataTable()
Dim connection As New SqlConnection("mystring")
Dim sqlCmd As New SqlCommand("SELECT EMPID FROM EMPEmployees WHERE EMPUserName = '" & WelcomeLabel.Text & "'", connection)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
If dt.Rows.Count > 0 Then
    EMPIDLabel.Text = dt.Rows(0)("EMPID").ToString()
End If

Can anyone fill me in on how to make this work?
Posted 13-Nov-12 5:56am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

None of this code should be in page load. IT should be in a data layer and CALLED from page prerender. The reason is that events fire after page load, prerender is the place to expect your data is in place and ready to load in to controls. Your code is not safe, I could erase your DB because you string mash SQL. Read up on SQL injection attacks and use procs or paramaterised queries. IF you want help, you need to define 'not working'. Does it blow up ? Have you stepped through it in the debugger ? Run the same SQL in SQL tools to see if you get the result you expect ?
Member 8197993 13-Nov-12 11:19am
I moved my code to prerender after i posted this, but i get the same results. when i load the page, "nothing happens" as in i get no error at all, but the label has no data in it.
Christian Graus 13-Nov-12 11:21am
Read all of my post. You are doing tons of things wrong, and none of my advice solves your problem, you've not told us enough for us to do that.
Christian Graus 13-Nov-12 11:21am
I suspect your SQL returns nothing. Do you know how to use the debugger ?
Mathlab 13-Nov-12 12:17pm
I was about to say the same thing, set a break point on the if test
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try to hard code the SQL command, cause i think you are passing the lable text value in the page load and at that time it has no value.

try it like this:
Dim sqlCmd As New SqlCommand("select command"

also try to put your code in page.ispostback if/then and see if its working


If Not Page.IsPostBack Then
// put your code here
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171207.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100