Link to home
Start Free TrialLog in
Avatar of OB1Canobie
OB1CanobieFlag for United States of America

asked on

Creating an Asynchronous Connection in asp.net vb

I have a form built in asp.net vb.  I am connecting to an SQL Server 2005 database through asp.net vb and having issues with adding information to a table.  I keep getting an error stating that I need to create an Asynchronous Connection.  I cannot figure a way to do this with my code below.I have attached the code that I need assistance with.  Thanks.
Private Sub Process_Information()        
        Reg_Ip = Request.ServerVariables("HTTP_XFORWARDED_FOR")
        If Reg_Ip = "" Then Reg_Ip = Request.ServerVariables("REMOTE_ADDR")
        Dim sqlConnection As New System.Data.SqlClient.SqlConnection
        sqlConnection.ConnectionString = "conn String here"
        Dim sqlCmd As New System.Data.SqlClient.SqlCommand
        sqlCmd.CommandType = Data.CommandType.Text
        sqlCmd.Connection = sqlConnection
        sqlCmd.CommandText = "INSERT INTO dbo_users " _
        & "(Reg_Date, Reg_Ip, Prefix, User_Name, Password, Password_Hint_Question, Password_Hint, First_Name, Middle_Initial, Last_Name) " _
        & "VALUES (@Reg_Date,@Reg_Ip,@Prefix,@User_Name,@Password,@Password_Hint_Question,@Password_Hint,@First_Name,@Middle,@Last_Name)"
        sqlCmd.Parameters.AddWithValue("@Reg_Date", Today())
        sqlCmd.Parameters.AddWithValue("@Reg_Ip", Reg_Ip)
        sqlCmd.Parameters.AddWithValue("@Prefix", dd_Prefix.SelectedValue)
        sqlCmd.Parameters.AddWithValue("@User_Name", tbx_Email.Text)
        sqlCmd.Parameters.AddWithValue("@Password", tbx_Password.Text)
        sqlCmd.Parameters.AddWithValue("@Password_Hint_Question", dd_Password_Hint_Question.SelectedValue)
        sqlCmd.Parameters.AddWithValue("@Password_Hint", tbx_Password_Hint.Text)
        sqlCmd.Parameters.AddWithValue("@First_Name", tbx_First_Name.Text)
        sqlCmd.Parameters.AddWithValue("@Middle", tbx_Middle_Initial.Text)
        sqlCmd.Parameters.AddWithValue("@Last_Name", tbx_Last_Name)
        sqlCmd.Connection.Open()
        sqlCmd.BeginExecuteNonQuery()
        sqlCmd.Connection.Close()
 
    End Sub

Open in new window

Avatar of Bobaran98
Bobaran98
Flag of United States of America image

I'm not sure about the Asynchronous error, but just to make sure, are you confident all your form field ids are correct?  One thing that jumps out at me as a possible error is the AddWithValue() for @Last_Name.  You've got:

sqlCmd.Parameters.AddWithValue("@Last_Name", tbx_Last_Name)
Shouldn't it be:

sqlCmd.Parameters.AddWithValue("@Last_Name", tbx_Last_Name.Text)
???  Let me know! :-)
Hello ,

What is your connection string ?

Please add following to your connection string  "Asynchronous Processing=true"

for example

connectionstring ="server=localhost;integrated security=true;initial catalog=test;Asynchronous Processing=true"




Private Sub Process_Information()        
        Reg_Ip = Request.ServerVariables("HTTP_XFORWARDED_FOR")
        If Reg_Ip = "" Then Reg_Ip = Request.ServerVariables("REMOTE_ADDR")
        Dim sqlConnection As New System.Data.SqlClient.SqlConnection
        sqlConnection.ConnectionString = "conn String here"
        Dim sqlCmd As New System.Data.SqlClient.SqlCommand
        sqlCmd.CommandType = Data.CommandType.Text
        sqlCmd.Connection = sqlConnection
        sqlCmd.CommandText = "INSERT INTO dbo_users " _
        & "(Reg_Date, Reg_Ip, Prefix, User_Name, Password, Password_Hint_Question, Password_Hint, First_Name, Middle_Initial, Last_Name) " _
        & "VALUES (@Reg_Date,@Reg_Ip,@Prefix,@User_Name,@Password,@Password_Hint_Question,@Password_Hint,@First_Name,@Middle,@Last_Name)"
        sqlCmd.Parameters.AddWithValue("@Reg_Date", Today())
        sqlCmd.Parameters.AddWithValue("@Reg_Ip", Reg_Ip)
        sqlCmd.Parameters.AddWithValue("@Prefix", dd_Prefix.SelectedValue)
        sqlCmd.Parameters.AddWithValue("@User_Name", tbx_Email.Text)
        sqlCmd.Parameters.AddWithValue("@Password", tbx_Password.Text)
        sqlCmd.Parameters.AddWithValue("@Password_Hint_Question", dd_Password_Hint_Question.SelectedValue)
        sqlCmd.Parameters.AddWithValue("@Password_Hint", tbx_Password_Hint.Text)
        sqlCmd.Parameters.AddWithValue("@First_Name", tbx_First_Name.Text)
        sqlCmd.Parameters.AddWithValue("@Middle", tbx_Middle_Initial.Text)
        sqlCmd.Parameters.AddWithValue("@Last_Name", tbx_Last_Name)
        sqlCmd.Connection.Open()
        dim ar as IAsyncResult = sqlCmd.BeginExecuteNonQuery()
        while Not ar.IsCompleted
        End While
        int effected = sqlCmd.EndExecuteNonQuery();  
        sqlCmd.Connection.Close()
         
    End Sub

Open in new window

I think jinal is on the right track...why not just:

sqlCmd.ExecuteNonQuery();
Avatar of OB1Canobie

ASKER

jinal:

What is the line int effected = sqlcmd.endexecutenonquery(); ??

What is this doing?  I've got an error on this line.  Please help.
ASKER CERTIFIED SOLUTION
Avatar of jinal
jinal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If still error then please put error detail over here.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jinal, your code worked.  Bobaran92, thanks for you assistance.  Did not see the tbx_Last_Name did not have an assigned value.