EDN Admin
Well-known member
i have a form with a datagrid and a search toolstrip. the dtagrid refreshes when i click the button, but the search doesnt work. but when i remove
<pre class="prettyprint lang-vb" style="font-size:12px DataGridView1.DataSource = bindingSource1 [/code]
from the load, then the search works but refresh doesnt. wht may be the error here? thanks in advance
<pre class="prettyprint lang-vb Imports System.Data.SqlClient
Public Class Product
Dim mycommand As SqlCommand
Dim ra As Integer
Dim ra1 As Integer
Dim dr As SqlDataReader
Dim myconnection As SqlConnection
Dim sql As String
Dim item As String
Dim adapter As New SqlDataAdapter
Dim j As Integer
Dim i As Integer
Dim bindingSource1 As New BindingSource()
Dim dataAdapter As New SqlDataAdapter()
Private Sub Product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TODO: This line of code loads data into the StockDataSet.stock table. You can move, or remove it, as needed.
Me.StockTableAdapter.Fill(Me.StockDataSet.stock)
DataGridView1.DataSource = bindingSource1
GetData("select * from stock")
End Sub
Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click
Addproducts.ShowDialog()
End Sub
Private Sub edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edit.Click
Editproducts.ShowDialog()
End Sub
Private Sub SearchToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Me.StockTableAdapter.Search(Me.StockDataSet.stock, ProductToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles refresh.Click
GetData(Me.dataAdapter.SelectCommand.CommandText)
End Sub
Private Sub GetData(ByVal selectCommand As String)
Try
Specify a connection string. Replace the given value with a
valid connection string for a Northwind SQL Server sample
database accessible to your system.
Dim connectionString As String = "Data Source=.SQLEXPRESS;AttachDbFilename=C:UsersAdministratordocumentsvisual studio 2010ProjectskankukhkankukhkankukhDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Create a new data adapter based on the specified query.
Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)
Create a command builder to generate SQL update, insert, and
delete commands based on selectCommand. These are used to
update the database.
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Populate a new data table and bind it to the BindingSource.
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
Catch ex As SqlException
MessageBox.Show("Refresh Not Succeded ...!!!")
End Try
End Sub
Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
Deleteproduct.ShowDialog()
End Sub
Private Sub FillByproductToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByproductToolStripButton.Click
Try
Me.StockTableAdapter.FillByproduct(Me.StockDataSet.stock, ProductToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class[/code]
<br/>
<hr class="sig shehzi
View the full article
<pre class="prettyprint lang-vb" style="font-size:12px DataGridView1.DataSource = bindingSource1 [/code]
from the load, then the search works but refresh doesnt. wht may be the error here? thanks in advance
<pre class="prettyprint lang-vb Imports System.Data.SqlClient
Public Class Product
Dim mycommand As SqlCommand
Dim ra As Integer
Dim ra1 As Integer
Dim dr As SqlDataReader
Dim myconnection As SqlConnection
Dim sql As String
Dim item As String
Dim adapter As New SqlDataAdapter
Dim j As Integer
Dim i As Integer
Dim bindingSource1 As New BindingSource()
Dim dataAdapter As New SqlDataAdapter()
Private Sub Product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TODO: This line of code loads data into the StockDataSet.stock table. You can move, or remove it, as needed.
Me.StockTableAdapter.Fill(Me.StockDataSet.stock)
DataGridView1.DataSource = bindingSource1
GetData("select * from stock")
End Sub
Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click
Addproducts.ShowDialog()
End Sub
Private Sub edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edit.Click
Editproducts.ShowDialog()
End Sub
Private Sub SearchToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Me.StockTableAdapter.Search(Me.StockDataSet.stock, ProductToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles refresh.Click
GetData(Me.dataAdapter.SelectCommand.CommandText)
End Sub
Private Sub GetData(ByVal selectCommand As String)
Try
Specify a connection string. Replace the given value with a
valid connection string for a Northwind SQL Server sample
database accessible to your system.
Dim connectionString As String = "Data Source=.SQLEXPRESS;AttachDbFilename=C:UsersAdministratordocumentsvisual studio 2010ProjectskankukhkankukhkankukhDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Create a new data adapter based on the specified query.
Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)
Create a command builder to generate SQL update, insert, and
delete commands based on selectCommand. These are used to
update the database.
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Populate a new data table and bind it to the BindingSource.
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
Catch ex As SqlException
MessageBox.Show("Refresh Not Succeded ...!!!")
End Try
End Sub
Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
Deleteproduct.ShowDialog()
End Sub
Private Sub FillByproductToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillByproductToolStripButton.Click
Try
Me.StockTableAdapter.FillByproduct(Me.StockDataSet.stock, ProductToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class[/code]
<br/>
<hr class="sig shehzi
View the full article