Hi guys I'm Indrajeet Roy, and I have a problem with a piece of code I am trying write in VB.net. I am using Visual Studio 2010 as my IDE.

The basic idea behind the form and the coding I am trying to do is to create a username, password and an access level number in an SQL Database I call VBDB.

The problem I have is that for some reason, when I execute the actual command to create the user, it tries to convert the password to int datatype!!

I am also posting the code for the form and the function below:

The code on the form is as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Acc_Lvl As Integer
    Dim user, passwd, var As String
    passwd = ""
    user = NameBox.Text
    If (String.Compare(PassBox.Text, PassBox2.Text)) = 0 Then
        passwd = PassBox.Text
        MsgBox("Password and Re Enter password fields don't match")
    End If
    var = ListBox1.SelectedItem
    Select Case var
        Case "Level 1 ( ACCESS EVERYTHING)"
            Acc_Lvl = 1
        Case "Level 2 (CAN NOT ADD AND DELETE USER)"
            Acc_Lvl = 2
            Acc_Lvl = 3
    End Select
    AddUserFun(Acc_Lvl, user, passwd)
    MsgBox("The User has been created")

The function is as follows:

Function AddUserFun(ByRef user, ByRef pass, ByRef acclvl) As Integer
    Dim i As Integer
        Dim conn As New SqlClient.SqlConnection("Server=.\SQLSERVER;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLSERVER\MSSQL\DATA\VBDB.mdf;Database=VBDB; Trusted_Connection=Yes;")
        Dim addusr As New SqlClient.SqlCommand
        addusr.CommandText = "INSERT INTO dbo.Users VALUES('" & user & "','" & pass & "','" & acclvl & "')"
        addusr.CommandType = CommandType.Text
        addusr.Connection = conn
    Catch ex As Exception
        i = 1
    End Try
    i = 0
    Return i
End Function

Please take a look at my coding and tell me where I'm making the error. This is my final semester project and this little thing is holding the whole thing up!!

Re: SQL Query in VB.Net tries to convert String to int 80 80

Please notice that you are calling your function on line 20:

AddUserFun(Acc_Lvl, user, passwd)

and your function declaration is:

Function AddUserFun(ByRef user, ByRef pass, ByRef acclvl) As Integer

Notice that the order of the parameters is different. Fix that, and I think you'll be fine.

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.