1

我只是 vb.net 中的新手,我已经搜索了谷歌,但我无法将这些与我的问题联系起来,或者说我仍然无法弄清楚......我收到错误以“从字符串“qwerty”转换为“ 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
4

2 回答 2

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)
于 2013-09-25T14:45:41.693 回答
0

我错过了 If Me.txtFirstName.Tag = 0 Then to Me.txtusername.Tag.ToString().Trim() = "0" 现在它的工作:)

于 2013-09-26T01:46:48.753 回答