Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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")
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 13-Nov-12 4:56am
Rate this: bad
good
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:
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
  Permalink  
Rate this: bad
good
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 ?
  Permalink  
Comments
Member 8197993 at 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 at 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 at 13-Nov-12 11:21am
   
I suspect your SQL returns nothing. Do you know how to use the debugger ?
Mathlab at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 389
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
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