What is the problem with the following login code?
Dim conn As SqlCommand = New SqlCommand("Data Source=RAHUL-034890AF0\SERVER2005;Initial Catalog=user_accounts;Integrated Security=True")
conn.Prepare()
Dim command As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand("select", conn.Connection)
command.Parameters(1).Value = txtusername.Text
command.Parameters(2).Value = txtpass.Text
Dim i As Integer = command.ExecuteNonQuery
conn.BeginExecuteNonQuery()
If Page.IsValid Then
Response.Redirect("~/salesbill.aspx")
Else
Lblerror.Text = "Username and password do not match"
End If
conn.Dispose()
I have tested the query and it works fine. The following is the error, which seems to be related to prepare statement. Im using SQL2005 and Vwd2008. The following is the error page:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 17: lbldate.Text = DateTime.Now
Line 18: Dim conn As SqlCommand = New SqlCommand("Data Source=RAHUL-034890AF0\SERVER2005;Initial Catalog=user_accounts;Integrated Security=True")
Line 19: conn.Prepare()
Line 20: Dim command As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand("select", conn.Connection)
Line 21: command.Parameters(1).Value = txtusername.Text
Source File: D:\sms_new\loginpage.aspx.vb Line: 19
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Data.SqlClient.SqlCommand.Prepare() +85
_Default.btnlogin_Click(Object sender, EventArgs e) in D:\sms_new\loginpage.aspx.vb:19
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746