Solved

Creating an Asynchronous Connection in asp.net vb

Posted on 2009-07-10
9
487 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:OB1Canobie
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24828647
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! :-)
0
 
LVL 15

Expert Comment

by:jinal
ID: 24828659
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"



0
 
LVL 15

Expert Comment

by:jinal
ID: 24828676

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

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 20

Expert Comment

by:ddayx10
ID: 24828700
I think jinal is on the right track...why not just:

sqlCmd.ExecuteNonQuery();
0
 

Author Comment

by:OB1Canobie
ID: 24831182
jinal:

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

What is this doing?  I've got an error on this line.  Please help.
0
 
LVL 15

Accepted Solution

by:
jinal earned 400 total points
ID: 24831231
Try this.

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
        Dim effected as Integer = sqlCmd.EndExecuteNonQuery(ar);  
        sqlCmd.Connection.Close()
         
    End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:jinal
ID: 24831232
If still error then please put error detail over here.
0
 
LVL 8

Assisted Solution

by:Bobaran98
Bobaran98 earned 100 total points
ID: 24831336
@jinal:  Is there something missing from that while loop?  Or is that simply a trick to throw code execution into a loop-- a wait-- until the execution completes?

@OB1Canobie:  Don't forget about that tbx_Last_Name.Text issue (line 21 in jinal's code).
0
 

Author Closing Comment

by:OB1Canobie
ID: 31602296
Jinal, your code worked.  Bobaran92, thanks for you assistance.  Did not see the tbx_Last_Name did not have an assigned value.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query to calculate avaerage 21 52
awk and Pythagoras? 5 19
ASP.Net MVC Login functions with my own Database and User Tables 2 25
ASP.NET (VB) return a record 2 34
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question