943,869 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 932
  • VB.NET RSS
Mar 25th, 2009
0

Cannot retrieve data from data base

Expand Post »
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.
vb.net Syntax (Toggle Plain Text)
  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
vb.net Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krm08 is offline Offline
11 posts
since Dec 2008
Mar 25th, 2009
0

Re: Cannot retrieve data from data base

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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nescio99 is offline Offline
2 posts
since Mar 2009
Mar 25th, 2009
0

Re: Cannot retrieve data from data base

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krm08 is offline Offline
11 posts
since Dec 2008
Mar 25th, 2009
0

Re: Cannot retrieve data from data base

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nescio99 is offline Offline
2 posts
since Mar 2009
Mar 25th, 2009
0

Re: Cannot retrieve data from data base

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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Mar 25th, 2009
0

Re: Cannot retrieve data from data base

Perhaps you can create another function to check whether it exists or not?

VB.net Syntax (Toggle Plain Text)
  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...
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How to kill the process in vb.net
Next Thread in VB.NET Forum Timeline: Pls Help, i have a problem loading my gridview with data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC