1

私はvb.netの初心者です。Googleを検索しましたが、それらを私の問題に関連付けることができません。または、まだ理解できないとしましょう...「文字列「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 を Username に変更しましたが、エラーが発生し、「.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 に答える