初始化字符串的格式不符合从索引0 |开始的规范。净

时间:2017-03-30 06:51:20

标签: .net vb.net

Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlParameter
Public Class VendorMain
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim dr As SqlDataReader
Dim strcon As String = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"

Private Sub loaddata()
    conn = New SqlConnection(strcon)
    conn.Open()
    Dim str As String = "SELECT * FROM vendortable"
    da = New SqlDataAdapter(str, conn)
    Dim ds As New DataSet
    da.Fill(ds, "vendortable")
    dgVendor.DataSource = ds.Tables("vendortable")
    da.Dispose()
    conn.Close()

End Sub
Private Sub loadnewdata()
    conn = New SqlConnection(strcon)
    conn.Open()
    Dim str As String = "SELECT * FROM vendortable"
    da = New SqlDataAdapter(str, conn)
    Dim ds As New DataSet
    da.Fill(ds, "vendortable")
    dgVendor.DataSource = ds.Tables("vendortable")
    da.Dispose()
    conn.Close()

End Sub
Private Sub LoadCombo()

    conn.Open()
    strcon = "select * from PAS"
    cmd = New SqlCommand(strcon, conn)
    dr = cmd.ExecuteReader()

    If dr.HasRows Then
        While dr.Read()
            mcPAS.Items.Add(dr("Products and Services"))
            mcUpdatePAS.Items.Add(dr("Products and Services"))
        End While

    End If

End Sub

Private Sub VendorMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    conn = New SqlConnection(strcon)
    conn.Open()
    loaddata()
    LoadCombo()
    loaditems()
    Timer1.Start()
End Sub


Private Sub dgVendor_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgVendor.CellDoubleClick
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.dgVendor.Rows(e.RowIndex)
        View.txtVendorID.Text = row.Cells("VendorID").Value.ToString
        View.txtSupplier.Text = row.Cells("Suppliers/Vendors").Value.ToString
        View.txtPAS.Text = row.Cells("Products and Services").Value.ToString
        View.txtAddress.Text = row.Cells("Address").Value.ToString
        View.txtFax.Text = row.Cells("Fax Number").Value.ToString
        View.txtOffice.Text = row.Cells("Office Number").Value.ToString
        View.txtContact.Text = row.Cells("Contact Person").Value.ToString
        View.txtOther.Text = row.Cells("Other Info/Details").Value.ToString
    End If
    View.Show()
End Sub
Private Sub loaditems()
    Dim con As New SqlConnection()
    con.ConnectionString = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"
    con.Open()
    Dim da As New SqlDataAdapter("select * from PAS", con)
    Dim table As New DataTable()
    da.Fill(table)
    mcAddPAS.DataSource = New BindingSource(table, Nothing)
    mcAddPAS.DisplayMember = "Products and Services"
    mcPAS.DataSource = New BindingSource(table, Nothing)
    mcPAS.DisplayMember = "Products and Services"
    mcUpdatePAS.DataSource = New BindingSource(table, Nothing)
    mcUpdatePAS.DisplayMember = "Products and Services"
    'write the column name which will be diplayed
    'you can even use  valueMember property, 
    'Names - DisplayMember - this is was you see in comboBox
    'IDs - ValueMember can be used as additional value of Person
    'column name for value
    conn.Close()
End Sub
Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
    Dim cmd As New SqlCommand
    Dim con As New SqlConnection
    Dim constr As String = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"

    Try
        con.ConnectionString = constr
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "Insert into PAS ([Products and Services]) values(@products)"
        cmd.Parameters.AddWithValue("@products", txtAddPAS.Text)
        cmd.ExecuteNonQuery()
        MetroFramework.MetroMessageBox.Show(Me, "Item Inserted!", "Confirmed", MessageBoxButtons.OK, MessageBoxIcon.Information)
        loaditems()
        con.Dispose()
        con.Close()
        txtAddPAS.Clear()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub btnDeleteItem_Click(sender As Object, e As EventArgs) Handles btnDeleteItem.Click
    Dim cummand As New SqlCommand
    Dim cunnection As New SqlConnection
    Dim constrng As String = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"
    Try
        cunnection.ConnectionString = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"
        cunnection.Open()
        cummand.Connection = cunnection
        cummand.CommandText = "Delete From PAS where PASID=@pasID"
        cummand.Parameters.Add(New SqlParameter("@pasID", lblItemID.Text))
        MetroFramework.MetroMessageBox.Show(Me, "Item Deleted!", "Confirmed", MessageBoxButtons.OK, MessageBoxIcon.Information)
        cummand.ExecuteNonQuery()
        loaditems()
        txtAddPAS.Clear()
    Catch ex As Exception
        MessageBox.Show(ex.Message)

    Finally
        cunnection.Close()
    End Try
End Sub

Private Sub mcAddPAS_SelectedIndexChanged(sender As Object, e As EventArgs) Handles mcAddPAS.SelectedIndexChanged
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim ds As New DataSet
    connetionString = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"
    connection = New SqlConnection(connetionString)
    Try
        connection.Open()
        adapter = New SqlDataAdapter("SELECT PASID from PAS where [Products and Services]='" & mcAddPAS.Text & "'", connection)
        adapter.Fill(ds)
        connection.Close()
        If ds.Tables(0).Rows.Count > 0 Then
            lblItemID.Text = ds.Tables(0).Rows(0)("PASID").ToString()
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles MetroButton1.Click
    'View.lblItemID.Text = View.dgItems.Item("ItemID", View.dgItems.CurrentRow.Index).Value()
    View.txtVendorID.Text = dgVendor.Item("VendorID", dgVendor.CurrentRow.Index).Value()
    View.txtSupplier.Text = dgVendor.Item("Suppliers/Vendors", dgVendor.CurrentRow.Index).Value()
    View.txtPAS.Text = dgVendor.Item("Products and Services", dgVendor.CurrentRow.Index).Value()
    View.txtAddress.Text = dgVendor.Item("Address", dgVendor.CurrentRow.Index).Value()
    View.txtFax.Text = dgVendor.Item("Fax Number", dgVendor.CurrentRow.Index).Value()
    View.txtOffice.Text = dgVendor.Item("Office Number", dgVendor.CurrentRow.Index).Value()
    View.txtContact.Text = dgVendor.Item("Contact Person", dgVendor.CurrentRow.Index).Value()
    View.txtOther.Text = dgVendor.Item("Other Info/Details", dgVendor.CurrentRow.Index).Value()
    View.Show()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    lbldatetime.Text = DateTime.Now.ToString
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Dim komand As New SqlCommand
    Dim connect As New SqlConnection("Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True")
    Try
        connect.ConnectionString = "Data Source=C1DSC-PC1\SQLEXPRESS;Initial Catalog=vendor;Integrated Security=True"
        connect.Open()
        komand.Connection = connect
        komand.CommandText = "Insert into vendortable ([Suppliers/Vendors],[Products and Services],Address,[Fax Number],[Office Number],[Contact Person],[Other Info/Details]) values(@suppliers,@pas,@address,@fax,@office,@contact,@other)"
        komand.Parameters.AddWithValue("@suppliers", txtAddVendor.Text)
        komand.Parameters.AddWithValue("@pas", mcPAS.Text)
        komand.Parameters.AddWithValue("@address", txtAddAddress.Text)
        komand.Parameters.AddWithValue("@fax", txtAddFax.Text)
        komand.Parameters.AddWithValue("@office", txtAddOffice.Text)
        komand.Parameters.AddWithValue("@contact", txtAddContact.Text)
        komand.Parameters.AddWithValue("@other", txtAddOther.Text)
        komand.ExecuteNonQuery()
        MetroFramework.MetroMessageBox.Show(Me, "Record Inserted!", "Confirmed", MessageBoxButtons.OK, MessageBoxIcon.Information)
        loaddata()
        connect.Dispose()
        connect.Close()


    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
End Class

这是我的代码。为什么我一直收到这个错误。我的connectionstring是否具有正确的格式? 我正在使用3个INSERT语句处理此系统。一个用于插入第一个表,第二个用于第二个表和第三个表。我总是使用这种方式将数据插入Microsoft SQL Server 2012.

1 个答案:

答案 0 :(得分:0)

首先,您可以从程序的开头放置换行符,以便我们可以找到错误发生的位置。然后把它还给我们。

谢谢