Need help displaying single database values

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Need help displaying single database values

 
0
  #1
Sep 27th, 2007
I need to figure out how to bind data and call it back anywhere on the page. Basically, this is what I am trying to do. Keep in mind that I am an asp programmer and am very new to ASP.NET.

I have build a custom login page that works. Now I need to retrieve the number of comments from a database and post it like I did in asp. Also, I need to retrieve a users id and post it in multiple places randomly on the page. Thanks.

I am converting an asp site to asp.net.

  1. Sub btnLogin_Click(S As Object, E As EventArgs)
  2. Dim loginValid As String
  3. Dim conLogin As OdbcConnection
  4. Dim cmdSelectLoginfo As OdbcCommand
  5. Dim dtrReaderLogin As OdbcDataReader
  6. Dim conStringLogin As String
  7. Dim SQLString As String
  8. Dim cmdSelectComments As OdbcCommand
  9. Dim dtrReaderComments As OdbcDataReader
  10. Dim SQLComments As String
  11. Dim strUAID As String
  12. Dim arow as datarow
  13. conStringLogin = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString")
  14. conLogin = New OdbcConnection( conStringLogin )
  15. SQLString = "SELECT UserID FROM Users WHERE UserName='" & txtUsername.Text & "' AND UserPassword='" & txtPassword.Text & "'"
  16. cmdSelectLoginfo = New OdbcCommand( SQLString, conLogin )
  17. conLogin.Open()
  18. dtrReaderLogin = cmdSelectLoginfo.ExecuteReader()
  19. if dtrReaderLogin.hasrows then
  20. conLogin.Close()
  21. Session("Login") = "Logged"
  22. hypAccount.NavigateURL = "/Vegas2/MyAccount/account.aspx?uaID=" & arow("UserID") & ""
  23. SQLComments = "SELECT COUNT(*) AS Comments FROM Comments WHERE ReadComment='No' AND UAID=" & arow("UserID") & ""
  24. cmdSelectComments = New OdbcCommand( SQLComments, conLogin )
  25. conLogin.Open()
  26. dtrReaderComments = cmdSelectComments.ExecuteReader()
  27. if dtrReaderComments.hasrows then
  28. hypComments.Visible = True
  29. hypComments.NavigateURL = "/Vegas2/MyAccount/comments.aspx?uaID=" & arow("UserID") & ""
  30. hypComments.Text = "New Comments!"
  31. else
  32. hypComments.Visible = False
  33. end if
  34. dtrReaderLogin.Close()
  35. dtrReaderComments.Close()
  36. conLogin.Close()
  37. else
  38. Session("Login") = "Failed"
  39. conLogin.Close()
  40. dtrReaderLogin.Close()
  41. end if
  42. If (Session("Login") = "Logged") then
  43. pnlLogin.Visible = False
  44. pnlLogged.Visible = True
  45. pnlForgotPass.Visible = False
  46. ElseIf (Session("Login") = "Failed") then
  47. pnlLogged.Visible = False
  48. pnlLogin.Visible = True
  49. pnlForgotpass.Visible = True
  50. Else
  51. pnlLogged.Visible = False
  52. pnlLogin.Visible = True
  53. pnlForgotpass.Visible = False
  54. End If
  55. End Sub
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #2
Sep 28th, 2007
Drag a GridView or Repeater control onto your form in design view. In the code behind (Do your databse code which you have shown you know how to do) then just pass the resulting reader to the DataSource property of the WebControl (GridView, Repeater whatever) and call it's DataBind() method.

Example:
  1. GridVeiw1.DataSource = dtrReaderComments;
  2. GridView1.DataBind();

That's basically it.

What worries me more is that this:
  1. SQLString = "SELECT UserID FROM Users WHERE UserName='" & txtUsername.Text & "' AND UserPassword='" & txtPassword.Text & "'"

Leaves you completely wide open to Sql Injection! Ecapsulate the login inplementation in a seperate class file with property accessors for loginname and password and use a stored procedure in a public method that returns a boolean for success or failure.

And this:
  1. hypAccount.NavigateURL = "/Vegas2/MyAccount/account.aspx?uaID=" & arow("UserID") & ""
Means I can assume the identity of anyone by randomly typing user id's in my address bar on the end of the querystring and causing mayhem!! Store the id in a session variable where it cannot be tampered with. Golden rule: Never never never never never under any circumstances what-so-ever blindly accept user input. Validate it *server* side not on the client and bin it if it's duff before it can do any harm.
Last edited by hollystyles; Sep 28th, 2007 at 9:34 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #3
Sep 28th, 2007
The querystring variable is just a cross reference. The uaid is stored in a session, but must match the querystring. It's just extra that is really, uneeded, but (to me anyway) provides better protection against hackers.

I was hoping I didn't have to use a datagrid or the like. I was hoping there was a way to declare the variable/string and recall it when needed. Seems just pointless extra code. Anyway, thanks a ton ^^

Oh, and for some reason maybe you can help on the sql part. I did the way I was supposed to by putting the following:

  1. SQLString = "SELECT UserID FROM Users WHERE UserName=@userName AND UserPassword=@userPass"
  2.  
  3. conComm.parameters.Add("@userName", txtUserName.Text)
  4. conComm.parameters.Add("@userPass", txtUserPass.Text)

But the authentication always failed. When I debugged by using response.write the SQL after opening it, it always appeared just as above, with the @userName and @userPass. It never substitued the required variables. Is there something else I need?

Oh and I am using OdbcConnection.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #4
Sep 28th, 2007
I was hoping I didn't have to use a datagrid or the like. I was hoping there was a way to declare the variable/string and recall it when needed. Seems just pointless extra code. Anyway, thanks a ton ^^
You'll have to expand on this, I'm not understanding it I'm afraid. In it's default config web controls on aspx pages keep their own data anyway via viewstate, is this what you're on about?

But the authentication always failed. When I debugged by using response.write the SQL after opening it, it always appeared just as above, with the @userName and @userPass. It never substitued the required variables. Is there something else I need?
I am assuming your actually passing the SQl String to the Command object? I don't see it in your code snippet. Also what database are you using? ODBC is like the trabant of data connection libraries. I think Odbc likes to have parameters like this:

  1. SQLString = "SELECT UserID FROM Users WHERE UserName=? AND UserPassword=?"
  2.  
  3. conComm.parameters.Add("@userName", txtUserName.Text)
  4. conComm.parameters.Add("@userPass", txtUserPass.Text)

And you have to add the params to the command's parameters collection in the exact order they appear in the Sql Statement.

P.S just using params does not make the input safe, you must use a RegularExpression validator or your own custom method Public Sub makeSafe(input As String) As String ... to weed out ; -- and words like DROP
http://msdn2.microsoft.com/en-us/library/bb355989.aspx
Last edited by hollystyles; Sep 28th, 2007 at 11:41 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #5
Sep 28th, 2007
Like, in ASP I could tap into the database once, and store the information in an array, or just say strField = rs("Field") and then call it any time on the page as <%= strField %>.

Heres the code for passing it along, and yes I am passing it to the command object:
  1. conStringLogin = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString")
  2. conLogin = New OdbcConnection( conStringLogin )
  3. SQLString = "SELECT UserID FROM Users WHERE UserName='" & txtUsername.Text & "' AND UserPassword='" & txtPassword.Text & "'"
  4. SQLString = "SELECT UserID FROM Users WHERE UserName=@userName AND UserPassword=@userPass"
  5. cmdSelectLoginfo = New OdbcCommand( SQLString, conLogin )
  6. cmdSelectLoginfo.Parameters.Add("@userName", txtUsername.Text)
  7. cmdSelectLoginfo.Parameters.Add("@userPass", txtPassword.Text)
  8. conLogin.Open()
  9. DA = New OdbcDataAdapter( SQLString, conLogin )
  10. DA.Fill(DS)
  11. 'Now how to bind it to a key to reference in sub queries?'
  12. strUAID = DA("UserID")

One main reason why I want a string to hold the value is due to me referencing it about 4 times. I would hate to keep a connection open that long also. And if you could answer this for me.. how would I reference a value in a sub query? I will be using UAID in a query to check comments. Is my best bet just grabbing the query, setting the session to it, then referencing the session? If so, I just need to know how to set it to the session variable in VB. like:
  1. DA.Fill(DS) 'after grabbing ONLY the UserID from the table, and only 1 userid is allowed.'
  2. Session("UAID") = ????

thanks. You're saving me hours of work.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #6
Sep 28th, 2007
Use ExecuteScalar() on the command object it is very efficient, returns just the first value in the first row of the result set. No need for an expensive DataAdapter and DataSet.

  1. conLogin.Open()
  2. strUAID = cmdSelectLoginfo.ExecuteScalar()
Like, in ASP I could tap into the database once, and store the information in an array
Just use a DataTable object
  1. Dim dt As DataTable
  2. con.Open()
  3. DA = New OdbcDataAdapter( SQLString, con )
  4. DA.Fill(dt)
  5. con.Close()
  6. DA.Dispose;
  7.  
  8. //dt is now available full of data but disconnected from the database
  9.  
  10. Dim strSomeVal As String
  11. strSomeVal = dt.Rows[0]["ColumnName"]

Those square brackets [] in that last statement may need to be parentheses () in VB syntax. I'm not sure cos VB syntax makes my teeth jangle ! yech!
Last edited by hollystyles; Sep 28th, 2007 at 12:07 pm.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #7
Sep 28th, 2007
Perfect! Thanks a ton!! I couldn't find this anywhere.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Need help displaying single database values

 
0
  #8
Sep 28th, 2007
Hey, thanks for all your help. This is my complete code... is there any bad stuff in here that could cause major problems with me, even with security? And do I have to close Scalars()? Thanks a ton, once again!
  1. Sub btnLogin_Click(S As Object, E As EventArgs)
  2. Dim loginValid As String
  3. Dim conLogin As OdbcConnection
  4. Dim cmdSelectLoginfo As OdbcCommand
  5. Dim dtrReaderLogin As OdbcDataReader
  6. Dim conStringLogin As String
  7. Dim SQLString As String
  8. Dim cmdSelectComments As OdbcCommand
  9. Dim dtrReaderComments As OdbcDataReader
  10. Dim SQLComments As String
  11. Dim strUAID As String
  12. Dim intComments As Integer
  13. conStringLogin = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString")
  14. conLogin = New OdbcConnection( conStringLogin )
  15. SQLString = "SELECT UserID FROM Users WHERE UserName=? AND UserPassword=?"
  16. cmdSelectLoginfo = New OdbcCommand( SQLString, conLogin )
  17. cmdSelectLoginfo.Parameters.Add("?userName", (txtUsername.Text.Trim()).ToString())
  18. cmdSelectLoginfo.Parameters.Add("?userPass", (txtPassword.Text.Trim()).ToString())
  19. conLogin.Open()
  20. dtrReaderLogin = cmdSelectLoginfo.ExecuteReader()
  21. if dtrReaderLogin.hasrows then
  22. dtrReaderLogin.Close()
  23. strUAID = cmdSelectLoginfo.ExecuteScalar()
  24. Session("UAID") = strUAID
  25. conLogin.Close()
  26. Session("Login") = "Logged"
  27. hypAccount.NavigateURL = "/Vegas2/MyAccount/account.aspx?uaID=" & strUAID & ""
  28. SQLComments = "SELECT COUNT(*) AS intComments FROM Comments WHERE ReadComment='No' AND UAID=" & strUAID & ""
  29. cmdSelectComments = New OdbcCommand( SQLComments, conLogin )
  30. conLogin.Open()
  31. intComments = cmdSelectComments.ExecuteScalar()
  32. if intComments >= 1 then
  33. hypComments.Visible = True
  34. hypComments.NavigateURL = "/Vegas2/MyAccount/comments.aspx?uaID=" & strUAID & ""
  35. if intComments > 1 then
  36. hypComments.Text = intComments & " New Comments!"
  37. else
  38. hypComments.Text = intComments & " New Comment!"
  39. end if
  40. else
  41. hypComments.Visible = False
  42. end if
  43. dtrReaderLogin.Close()
  44. conLogin.Close()
  45. else
  46. Session("Login") = "Failed"
  47. conLogin.Close()
  48. dtrReaderLogin.Close()
  49. end if
  50. If (Session("Login") = "Logged") then
  51. pnlLogin.Visible = False
  52. pnlLogged.Visible = True
  53. pnlForgotPass.Visible = False
  54. ElseIf (Session("Login") = "Failed") then
  55. pnlLogged.Visible = False
  56. pnlLogin.Visible = True
  57. pnlForgotpass.Visible = True
  58. Else
  59. pnlLogged.Visible = False
  60. pnlLogin.Visible = True
  61. pnlForgotpass.Visible = False
  62. End If
  63. End Sub
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC