ошибка: ExecuteNonQuery: свойство CommandText не было инициализировано - PullRequest
0 голосов
/ 31 января 2012

этот код находится в щелчке кнопки, я получаю все данные, используя разлитую, но я сталкиваюсь с ошибкой в ​​"cmd.CommandType = CommandType.Text"

 Dim conn As New SqlConnection(GetConnectionString())
            Dim sb As New StringBuilder(String.Empty)

            Dim splitItems As String() = Nothing
            For Each item As String In sc

                Const sqlStatement As String = "INSERT INTO Date (dateID,date) VALUES"
                If item.Contains(",") Then
                    splitItems = item.Split(",".ToCharArray())
                    sb.AppendFormat("{0}('{1}'); ", sqlStatement, splitItems(0))

                End If
            Next

            Try
                conn.Open()
                Dim cmd As New SqlCommand(sb.ToString(), conn)

                cmd.CommandType = CommandType.Text
                cmd.ExecuteNonQuery()


                Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
            Catch ex As System.Data.SqlClient.SqlException
                Dim msg As String = "Insert Error:"
                msg += ex.Message

                Throw New Exception(msg)
            Finally
                conn.Close()
            End Try

тот же код, ниже работа Dim connКак новый SqlConnection (GetConnectionString ()) Dim sb Как новый StringBuilder (String.Empty)

    Dim splitItems As String() = Nothing
    For Each item As String In sc

        Const sqlStatement As String = "INSERT INTO GuestList (groupID,guest,contact,eEmail,relationship,info,customerID) VALUES"
        If item.Contains(",") Then
            splitItems = item.Split(",".ToCharArray())
            sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2), splitItems(3), splitItems(4), splitItems(5), Session("customerID"))

        End If
    Next

    Try
        conn.Open()
        Dim cmd As New SqlCommand(sb.ToString(), conn)
        cmd.CommandType = CommandType.Text
        cmd.ExecuteNonQuery()


        Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
    Catch ex As System.Data.SqlClient.SqlException
        Dim msg As String = "Insert Error:"
        msg += ex.Message

        Throw New Exception(msg)
    Finally
        conn.Close()
    End Try

Ответы [ 3 ]

0 голосов
/ 18 июня 2013

Частные суб-формы Form1_Load (отправитель ByVal как System.Object, ByVal e как System.EventArgs) Обрабатывает

MyBase.Load

dim dt в качестве новой даты

   constr.Open()
    cmd = New OleDbCommand("SELECT *  FROM tblGender )
    da = New OleDbDataAdapter(cmd)
    da.Fill(dt)
    constr.Close()
    With ComboBox1
        .DataSource = dt
        .DisplayMember = "Gender"
    End With


    dim dt1 as new datatable

   constr.Open()
    cmd = New OleDbCommand("SELECT *  FROM tblStatus )
    da = New OleDbDataAdapter(cmd)
    da.Fill(dt)
    constr.Close()
    With ComboBox2
        .DataSource = dt1
        .DisplayMember = "Status"
    End With


    dim dt2 as new datatable
   constr.Open()
    cmd = New OleDbCommand("SELECT *  FROM tblDepartment )
    da = New OleDbDataAdapter(cmd)
    da.Fill(dt)
    constr.Close()
    With ComboBox3
        .DataSource = dt2
        .DisplayMember = "Department"
    End With

    End Sub
0 голосов
/ 20 декабря 2013

Смотрите это

Dim conn As New SqlConnection(GetConnectionString())
Dim sb As New StringBuilder(String.Empty)

Dim splitItems As String() = Nothing
For Each item As String In sc

    'Const sqlStatement As String = "INSERT INTO Date (dateID,date) VALUES"
    'If item.Contains(",") Then
    '    splitItems = item.Split(",".ToCharArray())
    '    sb.AppendFormat("{0}('{1}'); ", sqlStatement, splitItems(0))

    'End If

    Const sqlStatement As String = "INSERT INTO Date (dateID,date) VALUES"
    If item.Contains(",") Then
        splitItems = item.Split(",".ToCharArray())
        sb.AppendFormat("{0}({1},'{2}'); ", sqlStatement, splitItems(0), splitItems(1))

    End If

Next

Try
    conn.Open()
    Dim cmd As New SqlCommand(sb.ToString(), conn)

    cmd.CommandType = CommandType.Text
    cmd.ExecuteNonQuery()


    Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
Catch ex As System.Data.SqlClient.SqlException
    Dim msg As String = "Insert Error:"
    msg += ex.Message

    Throw New Exception(msg)
Finally
    conn.Close()
End Try
0 голосов
/ 31 января 2012

Вы никогда не устанавливаете свойство Command Text .
Вам вообще не нужно устанавливать Command Type .

...