Querying 2 tables using dataset in Visual basic - sql

I need help with my visual basic project. I have a Store.accdb database
which has 2 tables, customers and orders. I would like to be able to enter a customer's name (or part of a name) in a textbox and then display the name/s in a DataGridView when the search button is clicked. And then On a separate DataGridView I'd like to display the orders of the selected customer from my first DataGridView when the user clicks the Display button.
Edit: This is what the 2 tables look like
Customers table = custNum, custName, custAddress, custPhone
Orders table = orderNum, orderItem, custNum, price, qty
con.ConnectionString = dbProvider & dbSource
Try
con.Open()
sql = "SELECT custName FROM tblCustomers WHERE custName LIKE '%" & tbSearch.Text.ToUpper & "%'"
ds = New DataSet
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "CustomerDataset")
gridCustomers.DataSource = ds
gridCustomers.DataMember = "CustomerDataSet"
con.Close()
Catch ex As Exception
MessageBox.Show("Could not establish a connection", "Database Error")
End Try
This code works fine, it populates my customers datagridview. I get multiple rows whenever I only enter part of a customer's name. If I enter 'sm' in the textbox, it would display all customers with 'sm' in their name. How can I display the selected customer's (from 1st datagridview) orders in my 2nd datagridview when i click the display button? I hope I am making sense, english is not my first language so please bear with me.
Thanks!

You have to play with many events.
when selecting a customer from datagridview you have to use datagridview's selectedIndexChange event(assuming you have button to select in datagridview)
You have to use the same procedure in datagridview's selectedIndexChange event
con.ConnectionString = dbProvider & dbSource
Try
con.Open()
sql = "SELECT * FROM tblorders WHERE custName ='" & gridSelectedValue & "'"
//Assuming you have custName column in order table
ds = New DataSet
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "OrderDataset")
gridCustomers.DataSource = ds
gridCustomers.DataMember = "OrderDataset"
con.Close()
Catch ex As Exception
MessageBox.Show(ex.message)
End Try
Edit
If you dont have custName in order Then you should have CustNum in your query of DataGridView.

Step 1 - change the query that works. Instead of selecting just the customer name, select the customer id, or whatever the primary key of that table is.
Step 2 - when displaying your data, add an anchor tag with a query string that links to a new page showing the orders. The html should look like something like this:
Fred
In orders.aspx, call a query that brings back all the orders for customer_id = 1.
Oh, yes, by the way, convert all form and url variables to parameters before sending them to your database.

Actually, you only need to define a "Foreign Key" between the two tables, you can see this article to know how:
www.codeproject.com/Articles/28273/Xml-Database-Demo
Read the "Adding the DataTable Relation" section.
If you had made your db with Access, you have to the same thing. The same thing if you're using SQL.

Related

Update in access with vb.net

I'm making an invoice making software where I have 2 tables 1 for invoice and another for invoice items.
Now problem is if user wants to edit previous invoice and wanted to add some more items or remove some items I wrote query of update set but it needs to be insert also my update query updates all data to same as it is.
here is my update items query
Public Sub update_items()
ds.Clear()
str = ""
If cn.State = ConnectionState.Open Then
cn.Close()
End If
DBConfig.conn()
cn.Open()
For i As Integer = 0 To DataGridView1.Rows.Count - 2
cmd.Parameters.Clear()
cmd.Connection = cn
cmd.CommandText = "update invoice_items set [item_name]=#item_name,[qty]=#qty,[rate]=#rate where [invoice_id]=#invoice_id and [item_code]=#itemcode"
cmd.Parameters.AddWithValue("#item_name", DataGridView1.Rows(i).Cells(2).Value)
cmd.Parameters.AddWithValue("#qty", DataGridView1.Rows(i).Cells(3).Value)
cmd.Parameters.AddWithValue("#rate", DataGridView1.Rows(i).Cells(4).Value)
cmd.Parameters.AddWithValue("#invoice_id", invoice_id.Text.ToString)
cmd.Parameters.AddWithValue("#item_code", DataGridView1.Rows(i).Cells(1).Value)
cmd.ExecuteNonQuery()
Next
End Sub
my data is in datagridview which has one combobox too.Here is how my vb form looks like Here when page loads in side dgv data loads from table called item where item name is combobox and other calculation happens on bases of price and quantity. When it saves all of the data stores in to two diffrent tables one is invoice and second is invoice_items where I store data with item name, quantity, price, invoice no. Rest data is stored in invoice table.
Now where user wants to edit data he has to search with invoice no on search click I get data from invoice table as well as invoice_items and display as it is now I want to update all the data on update click data updates in invoice table perfectly but it starts problem with invoice_items table. which I had written before.
Golly, I suggest you consider using the dataset desinger. (or Entity frame work desinger). Dataset designer works much like EF, but it will auto matic bind and wire up for you automatic all of the data operations.
The results are VERY much like data bound ms-access forms.
so, if I use the data set designer, then I can drag + drop fields on to the form.
and you even get a navigation bar with record movement, and save, and add and delete features ALL automatic done for you.
So, you get say a form like this:
Same goes for datagrids - you can edit, add or even delete.
All of above works with without you having to write code.
Same goes for say a datagrid.
Say we drop in a button (to save), and then a data grid.
We have this:
Total code for this - inclding the save button code is now this:
Private Sub HotelGridEdit_Load(sender As Object, e As EventArgs) Handles Me.Load
LoadGrid()
End Sub
Sub LoadGrid()
Using conn As New OleDbConnection(My.Settings.AccessDB)
Dim strSQL As String =
"SELECT ID, FirstName, LastName, City, HotelName, Description, Active FROM tblHotelsA
ORDER BY HotelName"
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
Dim rstData As New DataTable
rstData.Load(cmdSQL.ExecuteReader)
DataGridView1.DataSource = rstData
End Using
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' save all edits, and new rows, and deletes
Using conn As New OleDbConnection(My.Settings.AccessDB)
Dim strSQL As String =
"SELECT ID, FirstName, LastName, City, HotelName, Description, Active FROM tblHotelsA
ORDER BY HotelName"
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
Dim da As New OleDbDataAdapter(cmdSQL)
Dim daC As New OleDbCommandBuilder(da)
da.Update(DataGridView1.DataSource)
End Using
End Using
End Sub
Note the save code. We send all edits, deletes, and even addtions in one shot.
So, you don't need (nor want) to write code just for a insert, just for edit, and just for adding). You can get the .net system and the form to do all of this work for you.

Visual Studio VB - Query from datatable instead of database

I'm a veteran Excel VBA coder who has been thrown into the deep end on this form <> Access database project. I'm working on one part of the form where users (employees) will select the site location (84 total, but not all will show based on what team they're on) and then select the department within that site. There are 2,189 total departments, so the department list should only show those for that site.
The database that we query from and update to is on a VERY slow server, so I want to pull a whole lookup table at the beginning (restricted down to those that team works on) into a datatable, then populate the first combo box with unique sites within that datatable, then populate the second combo box with all of the departments at that site.
The datatable is more or less just | ID | Site | Department |. I can fill that datatable just fine and leave it sitting in cache. And I could loop through the datatable to populate the combo boxes, but that's slower than it can be.
So is there a way to perhaps query from that datatable to either directly populate the combo boxes, or to fill a secondary datatable then to the combo boxes? For the first box, it's finding UNIQUE sites that is the challenge. Then for the second, it's departments where the site equals the one selected in the first box.
Thanks!
You can use Linq to Sql as mentioned by "Andrew Morton", i have made an exemple for you :
Dim da As SqlDataAdapter = New SqlDataAdapter("select * from YourDatatable, con)
Dim dt As New Data.DataTable()
da.Fill(dt)
'You can change here your condition as you need
Dim query = (From d1In dt.AsEnumerable() _
Where d1.Field(Of Integer)("ID") = 1 And d1.Field(Of Integer)
Select d1!ID, order!Site , d1!Department ,
If query.Count > 0 Then
comboBox1 .Items .Add (query(0).Site) 'Fill your Combobox1
end if
Dim query2 = (From d2 In dt.AsEnumerable() _
Where d2.Field(Of Integer)("ID") = 2 And d2.Field(Of Integer)
Select d2!ID, order!Site , d2!Department ,
If query2.Count > 0 Then
comboBox2 .Items .Add (query2(0).Department ) 'Fill your Combobox2
end if

Save all items of Listbox to access Database Table in VB.Net

I am working on a project and have hit a brick wall again. I have a listbox with some math operations in it (i.e. "1+2=3"). I am trying to save all items in the listbox to an access database, and I think I am getting close. The database has two entries I put in manually, but when I use the following code I only get -1's (plus the two manual entries) in the listbox (when I clear the listbox and load the database into the listbox). The other issue is I need to limit the database to 10 rows which I have no idea how to do. What I would like to do is check the database each time I save to it to see how many rows it has. If the number of listbox items I am saving will exceed 10 rows in the database then clear the database and start from 0 rows. Here is the code I have (which only attempts to save to database):
con.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = Mathops.accdb; Persist Security Info=False;"
Dim cmd As New OleDbCommand
Dim var1 As String
con.Open()
cmd.Connection = con
Try
For i As Integer = 0 To lstDisplay.Items.Count - 1
var1 = lstDisplay.Items.Item(i).ToString
cmd.CommandText = ("INSERT into Records (Operations) VALUES(" + var1 + ")")
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
Thank you for any assistance you can provide!
John
Oh...and the database has 2 fields; ID and Operations where ID is the Primary Key.
Try using listdisplay.items(i) to get the text from your listbox.
Then to sort out the 10 items issue:
If you do an SQL query:
SELECT Max(id) FROM Records that will give you the biggest id value in the table
Then check if the value you get back >= 10, and if it is use the SQL:
DELETE FROM Records to clear the table.

How to display records from a junction table in vb.net application?

i have two tables, Subjects and Departments, with a many to many relationship. The junction table is called Departments_Subjects_junction. i use sql server dbms.
Subjects:
subjects_id(primary key)
subject_name
Departments:
department_id(primary key)
department_name
Departments_Subjects_junction:
subject_id(primary key)
department_id(primary key)
I am able to store data in all the three tables.
Now i want to display the records for the user to see.
For eg, the user must see the list of all subjects for each department.
Something like this:
DepartmentA- Subject1, subject2, subject3, Subject4
DepartmentB- subject2, subject3, subject5
DeparmtentC- subject1, subject3, subject6.
How can i implement this in the best manner?using datagrid or listview or somethingelse? Please help. Thanks.
It doesn't matter what kind of control you use to display data, whether it's a datagrid or listview, both work fine and it all depends on how you want them to look. So I'm taking the liberty to say that your question really is about querying data against the different tables and showing it on a list. This is purely SQL.
Your table Departments_Subjects_junction cannot have both fields under it as primary keys since the way I see it, it's the table that defines what subjects a certain department has. So it should be...
Departments_Subjects_junction
department_id(primary key)
subject_id
Then you can query using this SQL statement...
SELECT T2.department_name, T3.subject_name
FROM Departments_Subjects_junction T1
INNER JOIN Departments T2
ON T1.department_id=T2.department_id
INNER JOIN Subjects T3
ON T1.subject_id=T3.subject_id;
Okay here's what you'll have to do...
1) Add a ListView control to your form.
2) Add a single column during design time.
3) Set the ListView's View Property to Details.
4) Add this code anywhere and just call it.
Private Sub displaydata()
Dim item As ListViewItem
Dim dt As New DataTable
dt.Clear()
ListView1.Groups.Clear()
da = New SqlDataAdapter("SELECT department_name FROM Departments", con)
da.Fill(dt)
For x = 0 To dt.Rows.Count - 1
ListView1.Groups.Add(dt.Rows(x).Item("department_name"), dt.Rows(x).Item("department_name"))
Next x
dt.Clear()
ListView1.Items.Clear()
da = New SqlDataAdapter("SELECT T2.department_name, T3.subject_name FROM Departments_Subjects_junction T1 INNER JOIN Departments T2 ON T1.department_id=T2.department_id INNER JOIN Subjects T3 ON T1.subject_id=T3.subject_id ", con)
da.Fill(dt)
For x = 0 To dt.Rows.Count - 1
item = New ListViewItem
item.Text = dt.Rows(x).Item("subject_name")
item.Group = ListView1.Groups(dt.Rows(x).Item("department_name"))
ListView1.Items.Add(item)
Next x
dt.Clear()
End Sub
This should work.

crystal report and sql commands

I have a problem in showing data content from joined table in crystal report my sql query is good and it shown my own data but i when fill crystal report datasource and show it the crystal repeat duplicate and more data my code is:
Dim rep As CrystalReport1 = New CrystalReport1()
Dim objcon = New SqlConnection("data source=(local);initial catalog=hesabres;user id='sa';password='Maysam7026'")
Dim objcom = New SqlCommand
Dim objdata As New DataTable
Dim objdr As SqlDataReader
objcom.CommandText = " SELECT customer.customer_name, customer.customer_tel, orders.order_stuff_name, orders.order_number" & _ " FROM hesabres.dbo.orders orders inner JOIN hesabres.dbo.customer customer ON orders.order_customer_id=customer.customer_id"
objcom.Connection = objcon
objcon.Open()
objdr = objcom.ExecuteReader
objdata.Load(objdr)
rep.SetDataSource(objdata)
CrystalReportViewer1.ReportSource = rep
in fact may bought one chair and ball and jahan bought one ball!
Crystal reports does not show any data itself.
If your database query result is fine then their is something with their code.
I will suggest that please review your code in detail.
Apply distinct keyword in select statement as below and then check it
SELECT Distinct customer.customer_name, customer.customer_tel,
orders.order_stuff_name, orders.order_number" & _ "
FROM hesabres.dbo.orders orders
inner JOIN hesabres.dbo.customer customer
ON orders.order_customer_id=customer.customer_id