从字符串“”转换为“double”无效

时间:2013-09-25 14:32:04

标签: vb.net

我只是vb.net中的新手,我搜索谷歌但我无法将这些连接到我的问题或者说我仍然无法想象...我得到错误“转换自字符串”qwerty “to'double'无效”..................... qwerty =用户名我真的很感激任何帮助。

Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty
    Try
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        If Me.txtusername.Tag = 0 Then
            sSQL = "INSERT INTO Instructor ( fname,  lname, mname, [password], [level], Username, Question, answer)"
            cmd.CommandText = sSQL
        Else
            sSQL = "UPDATE Instructor set fname = @first,  lname =  @last, mname = @initial, [password] = @pass, [level] =  @lvl, Question = @quest, answer = @ans WHERE  Username = @user"
            cmd.CommandText = sSQL
        End If
        cmd.Parameters.Add("@first", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtfname.Text)) > 0, Me.txtfname.Text, DBNull.Value)
        cmd.Parameters.Add("@last", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtlname.Text)) > 0, Me.txtlname.Text, DBNull.Value)
        cmd.Parameters.Add("@initial", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtinitial.Text)) > 0, Me.txtinitial.Text, DBNull.Value)
        cmd.Parameters.Add("@pass", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtpassword.Text)) > 0, Me.txtpassword.Text, DBNull.Value)
        cmd.Parameters.Add("@lvl", OleDbType.VarChar).Value = IIf(Len(Trim(Me.lbllevel.Text)) > 0, Me.lbllevel.Text, DBNull.Value)
        cmd.Parameters.Add("@quest", OleDbType.VarChar).Value = IIf(Len(Trim(Me.cmbquestion.Text)) > 0, Me.cmbquestion.Text, DBNull.Value)
        cmd.Parameters.Add("@ans", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtanswer.Text)) > 0, Me.txtanswer.Text, DBNull.Value)
        cmd.Parameters.Add("@user", OleDbType.VarChar).Value = txtusername.tag
        cmd.ExecuteNonQuery()

        If Me.txtusername.Tag = 0 Then
            cmd.CommandText = "Select @@Identity"
            Me.txtusername.Tag = cmd.ExecuteScalar()
        End If
        MsgBox("Data has been save.")
    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try

继承人完美的代码,但我将Contact_ID更改为用户名,但我得到了错误我认为它在“.tag”顺便说一下。 txtusername.tag实际上不是0 ..它已经有值,因此它只会选择UPDATE语句。

    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty
    Try
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        If Me.txtFirstName.Tag = 0 Then
            sSQL = "INSERT INTO tblAddressBook ( last_name,  mid_name, birth_date, gender, home_adr, bus_adr, tel_no, mobile_no, email)"
            sSQL = sSQL & "  VALUES(@last_name,  @mid_name, @birth_date, @gender, @home_adr, @bus_adr, @tel_no, @mobile_no, @email)"
            cmd.CommandText = sSQL
        Else
            sSQL = "UPDATE tblAddressBook set last_name = @last_name,  mid_name =  @mid_name, birth_date = @birth_date, gender = @gender"
            sSQL = sSQL & " ,home_adr = @home_adr, bus_adr = @bus_adr, tel_no = @tel_no, mobile_no = @mobile_no, email =  @email where contact_id = @id"
            cmd.CommandText = sSQL
        End If
        cmd.Parameters.Add("@last_name", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtLastName.Text)) > 0, Me.txtLastName.Text, DBNull.Value)
        cmd.Parameters.Add("@mid_name", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtMidName.Text)) > 0, Me.txtMidName.Text, DBNull.Value)

        cmd.Parameters.Add("@home_adr", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtHomeAdr.Text)) > 0, Me.txtHomeAdr.Text, DBNull.Value)
        cmd.Parameters.Add("@bus_adr", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtBusAdr.Text)) > 0, Me.txtBusAdr.Text, DBNull.Value)
        cmd.Parameters.Add("@tel_no", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtTelNo.Text)) > 0, Me.txtTelNo.Text, DBNull.Value)
        cmd.Parameters.Add("@mobile_no", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtMobNo.Text)) > 0, Me.txtMobNo.Text, DBNull.Value)
        cmd.Parameters.Add("@email", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtEmail.Text)) > 0, Me.txtEmail.Text, DBNull.Value)
        cmd.Parameters.Add("@id", OleDbType.Numeric).Value = Me.txtFirstName.Tag
        cmd.ExecuteNonQuery()

        'If the record is new then we have to get its ID so that we can edit it rightaway after the insertion.
        If Me.txtFirstName.Tag = 0 Then
            cmd.CommandText = "Select @@Identity"
            'Set textbox tag property with the ID of new record
            Me.txtFirstName.Tag = cmd.ExecuteScalar()
        End If
        MsgBox("Data has been save.")

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try

2 个答案:

答案 0 :(得分:1)

您错过了VALUES语句的INSERT部分,请尝试这样:

INSERT INTO Instructor (fname,  lname, mname, [password], [level], Username, Question, answer) 
VALUES (@fname, @last, @initial, @pass, @lvl, @user, @quest, @ans)

您也没有在参数中正确地投射Tag值:

cmd.Parameters.Add("@user", OleDbType.VarChar).Value = CType(txtusername.tag, String)

答案 1 :(得分:0)

我错过了如果Me.txtFirstName.Tag = 0然后到Me.txtusername.Tag.ToString()。修剪()=“0”,现在它的工作:)