I'm trying to fill either a combobox or a list with an sql query, I can get them to produce the number of entries pulled but not the names of the entries, and not multiple entries.
The code in question is simple:
Dim RegisterApt As New StudentsDataSetTableAdapters.TestTableAdapter
Try
txtTestPull.Items.Add(RegisterApt.FillByStudentsTest(StudentsDataSet.Test, StudentInsert.School, StudentInsert.School))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What I can't seem to find online is how to do this.
All I'd like to do is pull results using my sql query which I know works,
and push the resulting rows to the list or combobox
Here's the step by step on how to fill a ComboBox and a DataGridView using ADO.Net. I use the Northwind database as a sample.
1. Add a DataSet
Right-click your project and choose Add, then choose New Item. Choose Dataset in the next window.
2. Add a DataTable
Connect to your database and drag a table into the middle area. In this example, I choose the Customers table.
3. Add a query
Right-click your DataTable, and choose Add, then choose Query.
Choose Use SQL statements in the next window, click Next.
Choose SELECT which returns rows in the next window, click Next.
4. Write a query
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
FROM dbo.Customers
WHERE Country = #Country
I add a WHERE clause to filter the data. Click Next.
Gives names to your methods. I use FillByCountry in the first textbox and GetDataByCountry in the second textbox.
Save your project and build it first because you're adding a new DataSet.
5. Add some controls in your form
Add a Button, a TextBox, a ComboBox and a DataGridView. You can change the names, but I use the default names in this example.
6. Write some code to get the data and bind it to ComboBox and DataGridView
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' declare a DataTable
Dim dt As New DataSet1.CustomersDataTable
' declare a DataAdapter
Dim da As New DataSet1TableAdapters.CustomersTableAdapter
' use the DataAdapter to fill the DataTable
da.FillByCountry(dt, TextBox1.Text)
' bind the DataTable to a DataGridView
DataGridView1.DataSource = dt
' bind the DataTable to a ComboBox
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "CustomerID"
ComboBox1.DisplayMember = "CompanyName"
End Sub
End Class
7. Run the project and see the result
Related
I want to display a Table in crystal reports but its show only the last column, this is my code :
Try
Cursor = Cursors.WaitCursor
Dim id As Integer = 1
Dim Report1 As New PrintStockReport
Dim dt As DataTable = New DBConnect().selectdata( _
"SELECT StockTable.StockRef, StockTable.StockCat FROM StockTable;")
'Report1.SetDataSource(dt)
For i As Integer = 0 To dt.Rows.Count - 1
Report1.SetParameterValue("StockID", id)
Report1.SetParameterValue("StockRef", dt.Rows(i)(0).ToString)
Report1.SetParameterValue("StockCat", dt.Rows(i)(1).ToString)
id += 1
Next
CrystalReportViewer1.ReportSource = Report1
Cursor = Cursors.Default
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
but, when I set the Datasource its show " the report has no tables", can anyone explain to me why I get this issue? thanks
Report1.SetDataSource(dt)
First lets get your VB code fixed. Remove the For/Next loop completely, and then uncomment the line above it. Your code should now look like this:
Try
Cursor = Cursors.WaitCursor
Dim id As Integer = 1
Dim Report1 As New PrintStockReport
Dim dt As DataTable = New DBConnect().selectdata( _
"SELECT StockTable.StockRef, StockTable.StockCat FROM StockTable;")
Report1.SetDataSource(dt)
CrystalReportViewer1.ReportSource = Report1
Cursor = Cursors.Default
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
This will properly bind your datatable to the Report1 object as a data source. Now you need to fix your report in the designer by removing the parameter fields from the report. Parameter Fields are only used when you need the user to pass input into the report at runtime. For example, if I were making a report that shows data for a single financial period to the user and I want it to be flexible enough to work with any period, I would use a Parameter Field to ask the user to provide the period as input when the report is executed. If you look at the Field Explorer in your report designer you should see "Database Fields". When expanded this should show you the table or tables that are defined as the report's database. Expanding a table will then show you the list of fields available to the report. Drag the fields you want displayed on your report to the section where they should appear.
Once this is done, your report should work. This assumes you have your database defined within the database expert in the report designer though.
I'm working on a project where a form will pull information from an Access database. We have techs that are assigned to various stores, and instead of listing all of the stores and assigning a tech, I was looking to have a list of techs and assigning stores. Right now, the database has 4 fields.
Gate Tech - Text Field
Expiration Date - Date Field
Doors - Yes/No checkbox
Stores - Lookup & Relationship field that can have multiple selections
My form has a text box, search button, and a rich text box. If someone puts in a store number, it searches the Lookup & Relationship field and if that store is checked then it will put the tech in the rich text box.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text1 As String = TextBox1.Text
myConnection.Open()
Dim lstscmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1 WHERE Stores = #Stores", myConnection)
lstscmd.Parameters.AddWithValue("#Stores", CInt(text1))
dr = lstscmd.ExecuteReader
While dr.Read
RichTextBox1.AppendText(dr("Gate Tech").ToString + Environment.NewLine)
End While
myConnection.Close()
End Sub
At While dr.Read I get
Row handles must all be released before new ones can be obtained.
I would like it search the Stores field for any selected stores, and return the Gate Tech value to the rich text box.
Any help with this would be greatly appreciated.
I found where I was going wrong and the best way to solve it.
Instead of pulling the data from the table field, I created a query in access and pointed my search to that.
Dim lstscmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1 WHERE Stores = #Stores", myConnection)
lstscmd.Parameters.AddWithValue("#Stores", CInt(text1))
Replace Table1 (or your table name) with Query1 (or your query name) and it should start pulling values.
I have a combobox created and a table where I have first and last name both as columns. I want my columnbox to list the names of the first and last names put together and I'm not sure how to progress. I tried changing my DisplayMember to "First Name" but that only displays the first name when I run the program, I want BOTH.
This code was automatically added to my .vb file when I changed the displaymember to first name only:
Private Sub ComboBox3_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
End Sub
Is there something I must add here? I'll stay here diligently until I get an answer and answer questions.
.
.
.
.
.
.
.
.
.
.
.
.
.
ALSO
WHEN I Used a binding script to update my tables immediately, can I use a modified one to update the combobox immediately?
Private Sub BindGridSalespeople()
Dim constring As String = "server=classified;database=classified"
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM Salespeople", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
If you cannot add column to the DataSource used for filling ComboBox with values
Then add Computed Column in your SQL Table
ALTER TABLE YourTable ADD FullName AS (FirstName + ' ' + LastName);
Then use new column for DisplayMember in the combobox
ComboBox.DisplayMember = "FullName"
But I think adding column to the database only for UI purpose isn't best approach.
Exist much methods to add columns to the DataTable(Expression Column) if you using DataTable
Or adding column in the Select statement of SQL query
Or playing with properties of entities if you using Entities Framework
Hi I have a query which populates a datagridview which all works correctly. When the datagrid has been populated it has the following columns
UserID
UserName
UserDepot
I have a button which when pressed I would like to run another query based on the information in column UserID and whatever row is selected. I am having difficulty writing this query as I dont know what command I need to write in order to call this information
the datagrid is called dg_usersearch so I was expecting to use something like dg_usersearch.selectedvalue but selectedvalue isnt a member of datagridview does anyone know what command I should be using?
The query for filling the datagrid is as follows
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'creates a list of all incidents as per the search parameters
Dim dt As New DataTable
Dim query As String = "select [userid],[username],[userdepot] from [tbluser]"
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyDATASOURCE")
Using command As New OleDbCommand(query, connection)
Using adapter As New OleDbDataAdapter(command)
connection.Open()
adapter.Fill(dt)
connection.Close()
End Using
End Using
End Using
'checks to make sure query has results
If dt.Rows.Count > 0 Then
dgusersearch.DataSource = dt
'if no results display the following message
ElseIf dt.Rows.Count = 0 Then
MsgBox(Prompt:="No results for search request, please try again")
End If
Answered by #Plutronix above,
myDGV.SelectedRows(0).Cells(0).Value.ToString()
I have a Sql query on my form displaying all records having a duedate of the year 2013 in my tableA filling the datagrid view. And I have also a button and textbox.
What I wanted to do is to filter the result of my sql query on the datagrid view, Is it Possible to have another sql like this:
"SELECT caseno, duedate,remarks from (the data on my datagrid view)"
this is my datagrid sample:
caseno duedate remarks
1001 1/12/13 passed
1002 1/22/13 passed
1003 1/15/13 failed
1004 1/20/13 none
1005 1/06/13 failed
when I click the button I want to display on my datagrid all the records having a remarks that i typed on my textbox. anyone can help me? tnx! try to make it simple :)
Solution 1:
Set datasource of datagridview to select sql query
Bind data only when text change event occurs
Do a validation on the textbox text on button click event
Reference: How to filter records in datagrid view and show the selected record in the datagrid
Solution 2:
Check on this post on codeproject for Filter DataGridview with data entered in textbox
The important point to notice that you can use DataView's .RowFilter with the parameter you enter in the textbox. Then bind that DataView to the RowSource of GridView. In the above sample it's using:
dataview.RowFilter = "Year(Duedate) = " + textBox1.Text + ";
dataGridView1.DataSource = dataview;
You may also find lots of other articles as well here in SO.
your select statement for filtering remarks:
SELECT caseno, duedate, remarks FROM yourTable name where remarks = '"& txt1.text &"'
you can also make a combobox that consist of list of remarks and replace txt1.text to combobox1.text
to put it on a datagridview: (in your button event)
'declare your dataset and adapter
Dim adapter As MySqlDataAdapter
Dim ds As New DataSet
Try
connect()
adapter = New MySqlDataAdapter("SELECT caseno, duedate, remarks FROM yourTable name where remarks = '"& txt1.text &"'", con)
adapter.Fill(ds)
yourdatagridview.DataSource = ds.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
How about BindingSource.Filter
BindingSource.Filter = "caseno Like '" textBox.Text + "%'"
by using dataview row filter you can filter the data