How to redirect one selected item on data gridview to another data gridview in vb.net? - vb.net

I'm using vb.net. I have successfully query out this in data gridview by using sql
After I click the "Performance", I want to query out the details of "LM3409QHVMYUY8" and the details is inside the oracle and display into another data grid view
How I'm going to that ?
Thanks
I'm used this code to get the above datagridview:
Dim connectionString As String = System.Configuration.ConfigurationManager.AppSettings("connCommon")
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT * FROM ODS_Nspn_Pkg_Group_v WHERE ([SAG-Grp] = '" & ddlPackage & "')"
Dim sqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.CommandTimeout = 0
Dim dataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
Return dataSet

Related

Search by datagridview1 column1 values

i using this code to search by textbox to get the related data for the ID
Dim connetionString As String
Dim cnn As SqlConnection
connetionString="DataSource=IP;InitialCatalog=DB;UserID=sa;Password=password"
cnn = New SqlConnection(connetionString)
Dim com As String = "SELECT * FROM Table WHERE ID= '" & TextBox1.Text & "'"
Dim Adpt As New SqlDataAdapter(com, cnn)
Dim ds As New DataSet()
Adpt.Fill(ds, "Table")
DataGridView1.DataSource = ds.Tables(0)
What iam trying to do, i need to do the same code but with datagridview column1
Explaining:
i've already add button to upload data to datagridview the data include ID's
i need to search by those ID's in bulk, that included to the datagridview then post the income data to another datagridview,
Thanks in advance.
First, I think you should correct your existing code to use SQL Parameters.
Dim com As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com.Parameters.Add("#Id", SqlDbType.VarChar).Value = TextBox1.Text
Dim Adpt As New SqlDataAdapter(com)
Now to use the value in the first row, first column
Dim strId As String = DataGridView1.Rows(0).Cells(0).Value.ToString
Dim com2 As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com2.Parameters.Add("#Id", SqlDbType.VarChar).Value = strId
Dim Adpt2 As New SqlDataAdapter(com2)
I had to guess at the datatype. Please check your database to get the correct type. I guessed it was some type of string since you had it enclosed in single quotes.

vb.net Loading Images from Access Database to DataTable?

So I have a MS Access Database with 1 table (Records) and 2 fields in it ("RecordID" (Number), which is the primary key, and "LowRes" (OLE Object) which is a low Resolution image). There are about 100 records.
I/m trying to load the Access Table into a DataTable (ID_Table) in VB.net.
Code so far:
Dim cnString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_ID.accdb;"
Dim theQuery As String = "SELECT [RecordID], [LowRes] FROM [Records];"
Using CN As New OleDbConnection(cnString)
Dim command As New OleDbCommand(theQuery, CN)
Using objDataAdapter = New OleDbDataAdapter(command)
Dim ID_Table As New DataTable
CN.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
Dim picture As Image = Nothing
Using stream As New IO.MemoryStream(pictureData)
picture = Image.FromStream(stream)
objDataAdapter.Fill(ID_Table)
End Using
End Using
End Using
However the "DirectCast" command fails when I tell it to look at more then 1 field in my SQL statement with a datatype mismatch (if I just do [LowRes] it does not throw a error). However, I get stuck again when trying to apply the result to the table via the objDataAdapter, it doesnt fill the table with anything? I also notice that "picture" only contains the first image in the database.
I could put this database query in a function using "WHERE RECORDID=..." and loop it manually building the table returning "picture" each time, but Id like to avoid running a function 100 times, esp one that access a database.
Is it possible to read the whole database that contains images and just load it directly into a Datatable in one big swoop?
EDIT: So I got this to work:
Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=SBS2257_ID.accdb;"
Dim strSQL As String = "SELECT [RecordID], [LowRes] FROM [Records];"
Using objConnection = New OleDbConnection(strConnection)
Using objCommand = New OleDbCommand(strSQL, objConnection)
Using objDataAdapter = New OleDbDataAdapter(objCommand)
Dim objDataTable As New DataTable("IDs")
objDataAdapter.Fill(objDataTable)
Return objDataTable
End Using
End Using
End Using
how ever when I go to view row 0, col 1 which should be the first LowRes image via a .ToString Useing this code:
Private Sub PrintValues(ByVal table As DataTable)
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
MsgBox(row(col).ToString())
Next col
Next row
End Sub
It just displays "System.Byte[]". It knows its a Byte datatype, but how do I display that in a picturebox?
The ExecuteScalar() executes the query, and returns the first column of the first row in the result set returned by the query.
as your query is
Dim theQuery As String = "SELECT [RecordID], [LowRes] FROM [Records];"
the first column is RecordID which is not a Byte().
you can change your query as following:
Dim theQuery As String = "SELECT [LowRes] FROM [Records];"
or you have to use other methods to get data from the database
Dim strSql As String = "SELECT [RecordID], [LowRes] FROM [Records]"
Dim dtb As New DataTable
Using cnn As New OleDbConnection(connectionString)
cnn.Open()
Using dad As New OleDbDataAdapter(strSql, cnn)
dad.Fill(dtb)
End Using
cnn.Close()
End Using

Getting DevExpress Chart Series Values From Array VB.NET

I was trying to set DevExpress Chart Series from Data inside SQL Table.
Everything went fine except that the chart takes only the last attribute from the last series.
My code is:
con.Open() 'it opens SQL Connection
For i = 0 To (CheckedListBoxControl1.CheckedItems.Count - 1) 'list from ListBox
Lst.Add(CheckedListBoxControl1.CheckedItems(i).ToString) 'Putting Data in Array List
Dim Strl As String = String.Format("Select * from VPRogressCumulative where fname like '{0}' and lname like '{1}' order by id, no, CAST('1.' + date AS datetime)", ComboBox1.Text, Lst(i))
Dim sqlCom As New SqlCommand(Strl)
sqlCom.Connection = con
Dim myDA As SqlDataAdapter = New SqlDataAdapter(sqlCom)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "VPRogressCumulative")
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Next
con.Close()
I believe my Problem is in:
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Last two lines are giving the same series new attributes which I wasn't able to resolve the issue.
Your problem is here:
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
In each iteration of cycle you are creating the new DataSet and it's overrides previous DataSource in your ChartControl1. Your must use Series.DataSource instead of ChartControl.DataSource. Also you can use DataTable instead of DataSet.
Here is example:
'Your code:
'Dim myDataSet As DataSet = New DataSet()
'myDA.Fill(myDataSet, "VPRogressCumulative")
'ChartControl1.DataSource = myDataSet
'ChartControl1.DataAdapter = myDA
'Replace with this:
Dim myDataTable As DataTable = New DataTable("VPRogressCumulative")
myDA.Fill(myDataTable)
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
'Your code.
'ser.ArgumentDataMember = "VPRogressCumulative.Date"
'ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
'Replace with this:
ser.DataSource = myDataTable
ser.ArgumentDataMember = "Date"
ser.ValueDataMembers.AddRange(New String() {"Cumulative"})

Copy full row from a table's database

I'm using VB.Net and I have a database contains two tables "Personne" and "PersonneCopy" ( Same schema as Personne [4 columns = N°, FirstName, LastName, B_Day])
I load the first table in a Datagridview all it's fine with that, here's the code:
Dim Connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= .\BD.mdb")
Dim SqlRequete As String = "Select * From Personne"
Dim Adapter As New OleDbDataAdapter(SqlRequete, Connection)
Dim DataSet As New DataSet
Adapter.Fill(DataSet, "Tb1")
Dim DataTable As New DataTable
DataTable = DataSet.Tables("Tb1")
DataGridView1.DataSource = DataTable
Is there any way to add the full row that was selected from DataGridView1 to the table "PersonneCopy"?
And by the way I tried this code with an other Datagridview to get the index of the row selected and it works.
Dim Index As Integer = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value
Dim SqlRequete As String = "Select * From Personne where N°=" & Index.ToString
Dim Adapter As New OleDbDataAdapter(SqlRequete, Connection)
Dim Ds As New DataSet
Adapter.Fill(Ds, "Tb")
DataGridView2.DataSource = Ds.Tables(0)
Try like this
Dim drCopy as DataRow
Dim tbCopy as Datatable
tbCopy = .... 'PersonneCopy table
drCopy=tbCopy.NewRow()
For i as integer=0 to drCopy.columns.count-1
drCopy(i)=Ds.Tables(0).rows(0).items(i) '---------> or you can change rows(0)
Next
tbCopy.Rows.Add(drCopy)

VB.NET 2010 to connect to ms access database

Can someone show me how to connect vb.net 2010 to ms access database to get the data and display it in vb.net application that I am doing right now. My project is that I am doing dictionary application with vb.net so every time i put new word in search box, I want the vb.net to get the definition from ms access and display it in the application.
And code snippet would be great or tutorial
'Grabs data from a table and posts it into a ListView
Dim Table_ As String = "Table1"
Dim query As String = "SELECT * FROM " & Table_
Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
Dim ds As New DataSet
Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
cnn.Open()
Dim cmd As New OleDbCommand(query, cnn)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, Table_)
cnn.Close()
Dim t1 As DataTable = ds.Tables(Table_)
Dim row As DataRow
Dim Item(2) As String
For Each row In t1.Rows
Item(0) = row(0)
Item(1) = row(1)
Dim NextListItem As New ListViewItem(Item)
ListView1.Items.Add(NextListItem)
Next