| | |
Cannot retrieve data from data base
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
I'm using a class to open the database
Please help me it's really urgent.
Thanks in advance.
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)
Partial Class MasterPage Inherits System.Web.UI.MasterPage Public login As New Class1 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim u As String login.cmd.Connection = login.con login.cmd.CommandText = "select * from REG where user = '" + RTrim(TextBox1.Text) + "'" login.cmd.ExecuteNonQuery() login.rdr = login.cmd.ExecuteReader() If login.rdr.HasRows = True Then login.rdr.Read() u = login.rdr.Item("pwd") If RTrim(TextBox2.Text) = Trim(u) Then Response.Redirect("student1.aspx") End If Else MsgBox("Enter correct password", MsgBoxStyle.Critical) End If 'Try 'Catch ex As Exception ' MsgBox(ex.Message) 'End Try End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load login.connection() End Sub End Class
vb.net Syntax (Toggle Plain Text)
Imports Microsoft.VisualBasic Imports System.Data Imports System.Data.SqlClient Public Class Class1 Public con As SqlConnection Public cmd As New SqlCommand Public rdr As SqlDataReader Public Function connection() con = New SqlConnection("Data Source=ANGEL\SQLEXPRESS;Initial Catalog=swas;Integrated Security=True") con.Open() Return 0 End Function End Class
Please help me it's really urgent.
Thanks in advance.
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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)
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)
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
VB.NET Syntax (Toggle Plain Text)
cmd.commandtext = "SELECT count(*) FROM reg WHERE user= '" + RTrim(TextBox1.Text) + "' AND pwd = '" & RTrim(TextBox2.Text) & "' cmd.connection.open counter = cmd.ExecuteScalar cmd.connection.close if counter <= 0 then ... (user + pw combination unknown)
Or you could check if executeScalar lead to a DBNull.value
Good luck
Nescio (NN)
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.
If neither works, check that the command's connection object is actually valid and opened at the point you're executing the command.
Teme64 @ Windows Developer Blog
•
•
Join Date: Jun 2008
Posts: 89
Reputation:
Solved Threads: 7
Perhaps you can create another function to check whether it exists or not?
Hopes this help...
VB.net Syntax (Toggle Plain Text)
Dim conn As SqlConnection Dim strConn As String = "Data Source=ANX134\SQLEXPRESS;Initial Catalog=Dyslexia_Begin;Integrated Security=True" conn = New SqlConnection(strConn) conn.Open() SQLcmd = "select * FROM UserDetails where username = '" & txt_username.Text.Trim & "' and password = '" & txt_password.Text & "'" Public Function CheckExist(ByVal sSQL As String) As Boolean sqlcmd = New SqlCommand(sSQL, conn) DReader = sqlcmd.ExecuteReader() If DReader.Read() Then CheckExist = True Else CheckExist = False End If DReader.Close() End Function 'call this function in your code then...
Hopes this help...
![]() |
Similar Threads
- How to retrieve binary data from string (Java)
- How can i retrieve records from data base in data grid through unbound coding (Visual Basic 4 / 5 / 6)
- Send data on a serial port (C++)
- How to retrieve data from one form to another/ through Data Base (VB.NET)
- Tree View and thumbnail (VB.NET)
- error: The value for the useBean class attribute is invalid (JSP)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Data Base format in dotnet (ASP.NET)
Other Threads in the VB.NET Forum
- Previous Thread: How to kill the process in vb.net
- Next Thread: How to show Table data in Texbox
| Thread Tools | Search this Thread |
.net .net2008 2008 access advanced application array basic beginner browser button buttons center click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function generatetags html images input intel internet listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read regex remove right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol vb vb.net vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms winsock wpf wrapingcode xml year





