Cannot retrieve data from data base

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 11
Reputation: krm08 is an unknown quantity at this point 
Solved Threads: 0
krm08 krm08 is offline Offline
Newbie Poster

Cannot retrieve data from data base

 
0
  #1
Mar 25th, 2009
Hello friends,
I need an urgent help.I'm doing a project in asp.net which uses vb.net as back end language.I'm using sql 2005.My problem is that I can't retrieve the data from database,but I can insert the data into the table.
  1. Partial Class MasterPage
  2. Inherits System.Web.UI.MasterPage
  3.  
  4. Public login As New Class1
  5.  
  6.  
  7. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  8. Dim u As String
  9.  
  10. login.cmd.Connection = login.con
  11. login.cmd.CommandText = "select * from REG where user = '" + RTrim(TextBox1.Text) + "'"
  12. login.cmd.ExecuteNonQuery()
  13.  
  14. login.rdr = login.cmd.ExecuteReader()
  15.  
  16. If login.rdr.HasRows = True Then
  17. login.rdr.Read()
  18. u = login.rdr.Item("pwd")
  19. If RTrim(TextBox2.Text) = Trim(u) Then
  20. Response.Redirect("student1.aspx")
  21. End If
  22. Else
  23. MsgBox("Enter correct password", MsgBoxStyle.Critical)
  24.  
  25. End If
  26.  
  27. 'Try
  28.  
  29. 'Catch ex As Exception
  30. ' MsgBox(ex.Message)
  31.  
  32. 'End Try
  33. End Sub
  34.  
  35. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  36. login.connection()
  37.  
  38. End Sub
  39. End Class
I'm using a class to open the database
  1. Imports Microsoft.VisualBasic
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4.  
  5.  
  6. Public Class Class1
  7. Public con As SqlConnection
  8. Public cmd As New SqlCommand
  9. Public rdr As SqlDataReader
  10. Public Function connection()
  11. con = New SqlConnection("Data Source=ANGEL\SQLEXPRESS;Initial Catalog=swas;Integrated Security=True")
  12. con.Open()
  13. Return 0
  14.  
  15. End Function
  16. End Class

Please help me it's really urgent.
Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: nescio99 is an unknown quantity at this point 
Solved Threads: 0
nescio99 nescio99 is offline Offline
Newbie Poster

Re: Cannot retrieve data from data base

 
0
  #2
Mar 25th, 2009
why not use ExecuteScalar instead of executenonquery? ExecuteNonQuery does not return anything. When you make your select statement like "SELECT count(*) FROM reg WHERE user= '" + RTrim(TextBox1.Text) + "' AND pwd = '" & RTrim(TextBox2.Text) & "'
you know immediately whether user + pw combination exists (if count > 0).

(I am a beginner and this is my first reply, so please excuse me when not helpful)
Nescio (NN)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: krm08 is an unknown quantity at this point 
Solved Threads: 0
krm08 krm08 is offline Offline
Newbie Poster

Re: Cannot retrieve data from data base

 
0
  #3
Mar 25th, 2009
I have tried executescalar also,but it's not working.I checked it with breakpoints,the problem is that the "login.rdr.HasRows " always return false.I don't how it's happen,but I can insert into same table,but I can't retrieve the inserted data
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: nescio99 is an unknown quantity at this point 
Solved Threads: 0
nescio99 nescio99 is offline Offline
Newbie Poster

Re: Cannot retrieve data from data base

 
0
  #4
Mar 25th, 2009
  1. cmd.commandtext = "SELECT count(*) FROM reg WHERE user= '" + RTrim(TextBox1.Text) + "' AND pwd = '" & RTrim(TextBox2.Text) & "'
  2.  
  3. cmd.connection.open
  4. counter = cmd.ExecuteScalar
  5. cmd.connection.close
  6.  
  7.  
  8.  
  9. if counter <= 0 then ... (user + pw combination unknown)
  10.  

Or you could check if executeScalar lead to a DBNull.value

Good luck
Nescio (NN)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Cannot retrieve data from data base

 
0
  #5
Mar 25th, 2009
nescio99 has it right that you can't use ExecuteNonQuery() because you want to query something from the database. Either use ExecuteScalar() to check if a record exists i.e. returned number of records equals one, like nescio99 suggested, or if you need some information from the record, use simple Execute.

If neither works, check that the command's connection object is actually valid and opened at the point you're executing the command.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Cannot retrieve data from data base

 
0
  #6
Mar 25th, 2009
Perhaps you can create another function to check whether it exists or not?

  1.  
  2. Dim conn As SqlConnection
  3. Dim strConn As String = "Data Source=ANX134\SQLEXPRESS;Initial Catalog=Dyslexia_Begin;Integrated Security=True"
  4.  
  5. conn = New SqlConnection(strConn)
  6. conn.Open()
  7.  
  8. SQLcmd = "select * FROM UserDetails where username = '" & txt_username.Text.Trim & "' and password = '" & txt_password.Text & "'"
  9.  
  10. Public Function CheckExist(ByVal sSQL As String) As Boolean
  11. sqlcmd = New SqlCommand(sSQL, conn)
  12. DReader = sqlcmd.ExecuteReader()
  13. If DReader.Read() Then
  14. CheckExist = True
  15. Else
  16. CheckExist = False
  17. End If
  18. DReader.Close()
  19. End Function
  20.  
  21. 'call this function in your code then...

Hopes this help...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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