Click here to Skip to main content
15,909,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
G'day

I'm finally moving my DB from access to SQL - but joy to the world I need to write a new GUI. I'm getting there. It's a lot easier than I imagined.

I don't quite understand how I can retrieve values from my select query to place them into textboxes.

I think I have a basic understanding of it, but could someone tell me if I'm thinking the wrong way?

I need to dim the variables I want to receive from the select query 1st.
I need to run the SQL query to receive the values required after the where clause.

But, how do I reference the values to put them into the variables, which would then show I the textboxes.

Basically it's a repairs database that has a Primary Key (the RA) and it will only ever bring across one result.

On combobox.selectedindexchanged, it needs to run through the sql query that I've typed in and then it needs to be put into the variables.

I'm thinking the values would be stored as qrystr.row or something of that nature? Am I correct?

What I have so far is below

VB
Private Sub RA_IDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RA_IDComboBox.SelectedIndexChanged
       Dim connectionstring As New SqlConnection(Main.connectionstring)
       Dim qrystr As String = "SELECT        RA.RA_ID, RA.Till_ID, HardwareType.TillType, RA.Date_S, RA.Date_R, RA.Quote, RA.Accept, Supplier.Supplier, RA.Our_Cost, RA.Price, Customer.Company, " & _
                                            "Courier.CourCompany, RA.SendCon, RA.FixCon, RA.HIssue" & _
                                            "FROM            Courier INNER JOIN " & _
                                            "RA ON Courier.CourierID = RA.CourCompany INNER JOIN" & _
                                            "Customer ON RA.Company = Customer.CustomerID INNER JOIN" & _
                                            "Supplier ON RA.Supplier = Supplier.SupplierID INNER JOIN" & _
                                            "HardwareType ON RA.TillType = HardwareType.ID" & _
                                            "WHERE        (RA.RA_ID = N' " & RA_IDComboBox.Text & "')"
       Dim TillID As String
       Dim tilltype As String
       Dim customer As String
       Dim dates As String
       Dim supplier As String
       Dim courier As String
       Dim sendcon As String
       Dim hissue As String
Posted

1 solution

You can use data adapter to select/update data from DB

1. Declare your connection
2. Declare SQL data adapter
3. Declare data table
4. Fill the datatable


Here is the modified code

Private Sub RA_IDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RA_IDComboBox.SelectedIndexChanged

''I have removed SQL injection
''Use parameters always
Dim qrystr As String = "SELECT RA.RA_ID, RA.Till_ID, HardwareType.TillType, RA.Date_S, RA.Date_R, RA.Quote, RA.Accept, Supplier.Supplier, RA.Our_Cost, RA.Price, Customer.Company, " & _
"Courier.CourCompany, RA.SendCon, RA.FixCon, RA.HIssue" & _
"FROM Courier INNER JOIN " & _
"RA ON Courier.CourierID = RA.CourCompany INNER JOIN" & _
"Customer ON RA.Company = Customer.CustomerID INNER JOIN" & _
"Supplier ON RA.Supplier = Supplier.SupplierID INNER JOIN" & _
"HardwareType ON RA.TillType = HardwareType.ID" & _
"WHERE (RA.RA_ID = @RA_ID)"


Dim TillID As String
Dim tilltype As String
Dim customer As String
Dim dates As String
Dim supplier As String
Dim courier As String
Dim sendcon As String
Dim hissue As String
Using con As New SqlConnection(Main.connectionstring) ''Declare connection
Using da As New SqlDataAdapter(qrystr, con) ''Declare data adapter
''Add parameter and add value
da.SelectCommand.Parameters.Add("@RA_ID", SqlDbType.Int).Value = CInt(RA_IDComboBox.SelectedValue)
Using dtbl As New DataTable ''Declare data table
da.Fill(dtbl) ''Get data
If dtbl.Rows.Count > 0 Then
''You have data in datatable
''since you expect only 1 records from the select command
''You can use dtbl.Rows(0)

TillID = dtbl.Rows(0)("TillID").ToString()

tilltype = dtbl.Rows(0)("TillType").ToString()



End If


End Using

End Using

End Using

End Sub
 
Share this answer
 
Comments
Mendaharin 10-Sep-14 1:18am    
Cheers mate - that looks like it's going to work.
When I do run it though, it's coming up with an issue with the inner join syntax.
Quick question also - SQL injection is that the way I'd set the filter up?
does the @RA_ID
At the moment I'm just getting my head around the basics. After I figure out how to get everything else working, I will start looking at making it more secure. At the moment I just have a lot of if statements to make sure the formatting is correct, etc.
Abdul Samad KP 10-Sep-14 1:23am    
The SQL Syntax is correct, what error message you are getting?
Mendaharin 10-Sep-14 2:07am    
It just comes up with incorrect syntax near the keyword INNER
I'm trying to figure it out now, it doesn't make sense as I can pull that query into sql and it seems to work fine?
Mendaharin 10-Sep-14 2:09am    
Ahh I've just run through and pulled it into 1 line and it works. It's gotta be the way I'm concatenating (I hope that's the right term) string.
I shall continue :)
Abdul Samad KP 10-Sep-14 5:58am    
You have to add a space after the keyword INNER JOIN and also before the key word FROM

For example change the line "RA ON Courier.CourierID = RA.CourCompany INNER JOIN" & _
to "RA ON Courier.CourierID = RA.CourCompany INNER JOIN " & _

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900