S
shiknet12343
Guest
Am having problem in inserting data in datagrid
can sme1 help
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim connection As New Data.SqlClient.SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ShikshaKumariHURDOYA\Documents\SuperGroup.mdf;Integrated Security=True;Connect Timeout=30")
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand("SELECT * FROM StockItem", connection)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)
connection.Open()
Dim myTable As DataTable = New DataTable
adapter.Fill(myTable)
DataGridView1.DataSource = myTable
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim connection As New Data.SqlClient.SqlConnection
Dim adapter As New SqlDataAdapter()
Dim myTable = CType(DataGridView1.DataSource, DataTable)
adapter.Update(myTable)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New Data.SqlClient.SqlConnection
Dim command As New Data.SqlClient.SqlCommand
Try
connection.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ShikshaKumariHURDOYA\Documents\SuperGroup.mdf;Integrated Security=True;Connect Timeout=30"
command.CommandText = "INSERT INTO StockItem (ID, Name, Quantity,Unit) VALUES (@ID, @name, @Quantity, @Unit)"
command.Parameters.Add("@ID", SqlDbType.Int)
command.Parameters.Add("@name", SqlDbType.VarChar)
command.Parameters.Add("@Quantity", SqlDbType.Float)
command.Parameters.Add("@Unit", SqlDbType.VarChar)
connection.Open()
command.Connection = connection
For i As Integer = 0 To DataGridView1.Rows.Count - 1
command.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value
command.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters(3).Value = DataGridView1.Rows(i).Cells(3).Value
command.ExecuteNonQuery()
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Continue reading...
can sme1 help
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim connection As New Data.SqlClient.SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ShikshaKumariHURDOYA\Documents\SuperGroup.mdf;Integrated Security=True;Connect Timeout=30")
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand("SELECT * FROM StockItem", connection)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)
connection.Open()
Dim myTable As DataTable = New DataTable
adapter.Fill(myTable)
DataGridView1.DataSource = myTable
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim connection As New Data.SqlClient.SqlConnection
Dim adapter As New SqlDataAdapter()
Dim myTable = CType(DataGridView1.DataSource, DataTable)
adapter.Update(myTable)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New Data.SqlClient.SqlConnection
Dim command As New Data.SqlClient.SqlCommand
Try
connection.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ShikshaKumariHURDOYA\Documents\SuperGroup.mdf;Integrated Security=True;Connect Timeout=30"
command.CommandText = "INSERT INTO StockItem (ID, Name, Quantity,Unit) VALUES (@ID, @name, @Quantity, @Unit)"
command.Parameters.Add("@ID", SqlDbType.Int)
command.Parameters.Add("@name", SqlDbType.VarChar)
command.Parameters.Add("@Quantity", SqlDbType.Float)
command.Parameters.Add("@Unit", SqlDbType.VarChar)
connection.Open()
command.Connection = connection
For i As Integer = 0 To DataGridView1.Rows.Count - 1
command.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value
command.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters(3).Value = DataGridView1.Rows(i).Cells(3).Value
command.ExecuteNonQuery()
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Continue reading...