Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

VB
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")
connection.Open()
Dim sqlCmd As New SqlCommand("SELECT EMPID FROM EMPEmployees WHERE EMPUserName = '" & WelcomeLabel.Text & "'", connection)
Dim sqlDa As New SqlDataAdapter(sqlCmd)

sqlDa.Fill(dt)
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

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:
Quote:
Dim sqlCmd As New SqlCommand("select command"



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

Quote:

If Not Page.IsPostBack Then
// put your code here
End If
 
Share this answer
 
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 ?
 
Share this answer
 
Comments
PTG.Jake 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 ?
[no name] 13-Nov-12 12:17pm    
I was about to say the same thing, set a break point on the if test

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900