0

vb.net dataadapter、datatable、および datarow 機能を使用して、SQL データベースにレコードを挿入しようとしています。次のコードを使用しますが、エラーが発生します。

Object reference not set to an instance of an object

Imports System.Data.SqlClient
Public Class Form1  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        Try
            cn.Open()
            da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
            da.Fill(ds) 
            Dim dt As New DataTable
            dt = ds.Tables("emp_tbl")

            'Error in this line(Object reference not set to an  instance of an object)'
            Dim dr As DataRow = dt.NewRow() 

            dr.Item("emp_id") = TextBox1.Text.Trim
            dr.Item("emp_name") = TextBox2.Text.Trim
            dr.Item("salary") = TextBox3.Text.Trim
            dr.Item("age") = TextBox4.Text.Trim
            dr.Item("emp_group") = TextBox5.Text.Trim
            dt.Rows.Add(dr)
            da.Update(ds)
            MsgBox("Record Successfully Inserted")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class 
4

4 に答える 4

0

すべてうまくいきましたが、次の行を変更します。

da.Update(ds)

次のように:

Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
    ESCBuilder.GetUpdateCommand()
    da.UpdateCommand = ESCBuilder.GetUpdateCommand()
    da.Update(ds)
于 2013-12-11T14:11:17.653 に答える
0

これをチェックしてください: Dim dr As DataRow = new dt.NewRow()

于 2013-05-24T14:33:37.387 に答える
0
Imports System.Data.SqlClient
Public Class Form4
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim str As String
    Dim count As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
        con.Open()
      ‘  cmd = New SqlCommand("select * from Doctor", con)
        str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
        cmd = New SqlCommand(str, con)
        count = cmd.ExecuteNonQuery()
        MessageBox.Show(count & " Record inserted")

       con.close()
    End Sub

Imports System.Data.SqlClient
Public Class Form4
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim str As String
    Dim count As Integer
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
     con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
        con.Open()
        cmd = New SqlCommand("select * from Patient", con)
        cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
        cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
        cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
        cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
‘you can take any row in your program
        count = cmd.ExecuteNonQuery()
        MessageBox.Show("Record Deleted")
    End Sub
于 2014-04-12T04:14:11.347 に答える