0

I want to retrieve data from SQL Server and display into Textbox and DataGrid. I want the data from request box to be display into the textbox and the leftjoin table to be diplayed in datagrid.

 con.Open()
    cmd.Connection = con
    cmd.CommandText = "select * from requestbox left join requisitiondata on requisitiondata.requestdata_id = requisitiondata.requestdata_id where request_box = '" & txtsearch.Text & "'"

    cmd.ExecuteNonQuery()

    Dim TABLE As New DataTable

    With da
        .SelectCommand = cmd
        .Fill(TABLE)
    End With


    cbspayment.Text = ("spayment").ToString()
    cbsoption.Text = ("soption").ToString()
    txtto.Text = ("to1").ToString()
    txtsupplier.Text = ("supplier").ToString()
    txtterms.Text = ("terms").ToString()
    txtreference.Text = ("reference").ToString()
    txtfrom.Text = ("from1").ToString()
    txtcharge.Text = ("charge").ToString()
5
  • Ronel you don't appear to know SQL, it seems you are setting some SQL variables in the statement and then you expect somehow to get the same value back into the textbox. Commented Oct 30, 2020 at 1:46
  • Why are you calling ExecuteNonQuery when you want to execute a query and are already doing so when you call Fill? Commented Oct 30, 2020 at 2:06
  • What table does the field request_box belong to? Commented Oct 31, 2020 at 4:21
  • NEVER concatenate strings with user input for sql statements. Always use parameters to avoid sql injection. Commented Oct 31, 2020 at 4:23
  • What database is this? I'll take a wild guess. Access? Commented Oct 31, 2020 at 4:24

1 Answer 1

0

If you only want some columns of your data to be displayed in your grid then you should add the desired columns to the grid in the designer. You can specify what column in the data source a grid column should bind to by setting its DataPropertyName. You can then bind your data to the grid and other controls as well if desired. As an example:

  1. Add a DataGridView, a BindingSource and a TextBox to your form.
  2. Add a single text box column to the grid and set its DataPropertyName to "Name".
  3. Create a handler for the Load event of the form and add the following code:
Dim table As New DataTable

With table.Columns
    .Add("Name", GetType(String))
    .Add("Description", GetType(String))
End With

With table.Rows
    .Add("One", "First")
    .Add("Two", "Second")
    .Add("Three", "Third")
End With

BindingSource1.DataSource = table
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = BindingSource1
TextBox1.DataBindings.Add("Text", BindingSource1, "Description")

When you run the project, you will see the Name column's data displayed in the grid and, when you select a row, you will see the corresponding value from the Description column displayed in the TextBox. You can apply the same principle no matter how many columns you want displayed in the grid, no matter how many TextBoxes you have and no matter how you populate your DataTable.

Sign up to request clarification or add additional context in comments.

2 Comments

just want to ask about the rows...what is that mean the "One","First","Two","Second","Three","Third"
@Ronel, there's no "meaning". It's just example data. The Rows.Add method adds a new row with the two specified values in the two columns of the DataTable. The actual data could be anything at all. I just chose something simple that would enable you to see the principle in action. Your columns and data will be determined by your database query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.