473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to filter data and then update it in datagridview?

I wanted to filter the data, make changes and the save back to the database.
The SQL data adapter and SQL dataset are clashing with TableDataAdapter i.e., db1TableAdpater and DatabseDataSet i.e GreetingDataset.
I wanted to make them working for the above purpose.

I am also open for other methods of executing the code.

Please help!

Here's the code.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Console
  3. Imports System.Data.SqlClient
  4. Imports System.Data.OleDb
  5.  
  6. Public Class Form1
  7.  
  8.     Dim sqlcon As New SqlConnection
  9.     Dim ds As New DataSet
  10.     Dim dc As SqlCommand
  11.     Dim daa As New SqlDataAdapter
  12.     Dim bs As New BindingSource
  13.     Public Const ConnectionString As String = "Data Source=SQLORACLE;Initial Catalog=greeting;User ID=classic;password=stripes"
  14.  
  15.  
  16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.         'TODO: This line of code loads data into the 'GreetingDataSet.db1' table. You can move, or remove it, as needed.
  18.         'Me.Db1TableAdapter.Fill(Me.GreetingDataSet.db1)
  19.  
  20.         sqlcon.ConnectionString = "Data Source=SQLORACLE;Initial Catalog=greeting;User ID=classic;password=stripes"
  21.         Try
  22.             sqlcon.Open()
  23.         Catch ex As Exception
  24.             MessageBox.Show(ex.Message, "Connection to H247 Failed", MessageBoxButtons.OK)
  25.         End Try
  26.         If sqlcon.State = 1 Then
  27.             Me.Text = "Connection successful"
  28.         End If
  29.         Call populate_combo(cboest, "ESTNO")
  30.         cboest.Sorted = True
  31.         cboest.Enabled = False
  32.         cbover.Enabled = False
  33.         Db1DataGridView.Visible = False
  34.         Db1BindingNavigator.Visible = False
  35.     End Sub
  36.  
  37.     Sub populate_Adapter_Dataset()
  38.         Dim daa As SqlDataAdapter
  39.         daa = New SqlClient.SqlDataAdapter("Select * from db1 where ESTNO = '" & cboest.Text & "' AND ESTVERSION = '" & cbover.Text & "' ", sqlcon)
  40.         daa.Fill(ds, "db1")
  41.  
  42.         Db1DataGridView.DataSource = ds.Tables("db1")
  43.         Me.Db1TableAdapter.Fill(Me.GreetingDataSet.db1)
  44.     End Sub
  45.  
  46.     Public Sub populate_combo(ByRef cmb1 As ComboBox, ByVal cbest As String)
  47.         dc = New SqlCommand("Select distinct " & cbest & " from db1", sqlcon)
  48.         Dim da As SqlClient.SqlDataReader = dc.ExecuteReader
  49.         cmb1.Items.Clear()
  50.         While da.Read
  51.             cmb1.Items.Add(da("" & cbest & ""))
  52.         End While
  53.         da.Close()
  54.     End Sub
  55.  
  56.     Public Sub populate_combo1(ByRef cmb2 As ComboBox, ByVal cbver As String)
  57.         dc = New SqlCommand("Select distinct estversion from db1 where estno = '" & cboest.Text & "' ", sqlcon)
  58.         Dim da As SqlClient.SqlDataReader = dc.ExecuteReader
  59.         cmb2.Items.Clear()
  60.         While da.Read
  61.             cmb2.Items.Add(da("" & cbver & ""))
  62.         End While
  63.         da.Close()
  64.     End Sub
  65.  
  66.     Private Sub Db1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Db1BindingNavigatorSaveItem.Click
  67.         Me.Validate()
  68.         Me.Db1BindingSource.EndEdit()
  69.         Me.Db1TableAdapter.Update(Me.GreetingDataSet.db1)
  70.  
  71.     End Sub
  72.  
  73.     Private Sub cmdselect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdselect.Click
  74.         cboest.Enabled = True
  75.         cmdselect.Enabled = False
  76.     End Sub
  77.  
  78.     Private Sub cboest_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboest.SelectedIndexChanged
  79.         cbover.Enabled = True
  80.         Call populate_combo1(cbover, "estversion")
  81.     End Sub
  82.  
  83.     Private Sub cbover_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbover.SelectedIndexChanged
  84.         Call populate_Adapter_Dataset()
  85.         cboest.Enabled = False
  86.         cbover.Enabled = False
  87.         cmdselect.Enabled = True
  88.         Db1DataGridView.Visible = True
  89.         Db1BindingNavigator.Visible = True
  90.     End Sub
  91. End Class
  92.  
Jan 19 '11 #1
0 1040

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
7
by: Greg P | last post by:
I'm new to VS2005 and want to simply update my data with the dataGridView that was generated when I draged my query from the Data Sources Pane. I think I may need to create a Data Adaptor but I'm...
10
by: pauljohns353 | last post by:
Hi, I am new to VB.net programming and was wondering if anybody has any suggestions as to how I can export data from a DataGridView via a command button to a CSV file. Any suggestions or sample...
7
by: Arne Beruldsen | last post by:
When I go to update my datagridview...I get this error Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information. ...
0
by: David Mayerovitch | last post by:
As an exercise in learning Visual Basic 2005, I am putting together a simple XML editor. I place on the form the objects DataGridView1 and DataSet1. ' Read an XML file into DataSet1:...
1
by: ralucutza77 | last post by:
Hello, I’m a begginer and I know that my question might sound stupid…So, what I’m trying to do, in a windows application, is connect to a SQL data base and display in a datagridview 4 columns from...
4
by: Anthoni | last post by:
Dear All, I need to export data of unbound datagridview to XML file. Since my DataSource is NULL, What other options do I have? Does i need to make 2 loops (on all the records and inside loop of...
1
by: dillipdhal | last post by:
Hi This is Dillip from HSBC Software,Hyd. Now I m working in a financial domain. Please help me in "How can I insert data from a datagridview to database in vb .net and also retrive the data "
0
by: Pascal | last post by:
Hello I began with vbnet and databinding. I created a database containing a table access Students. In a form frmListEleves I slipped and deposited the table in question and a datagridview was...
2
by: Huy Nguyen | last post by:
Hello, How can i force the cell to accept the input for now and flag it as error for user to correct it later. For Example: case 1. I have a column that is NOT bound to database table, then I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.