Adding code to a visual basic 2005 button to run a sql query

Marig

Adding code to a visual basic 2005 button to run a sql query

by Marig » Wed, 17 Nov 2010 06:04:44

Ok
Im trying to develop a form in which the user inputs a number and the information associated in that number (columns in the db ) in various textboxes (or labels ..im desperate to get the information) are displayed after a button is clicked. I know you have to bind the textboxes that i want to fill with the appropriate columns in the db.. but
1) dont know what code to use ( or at least someplace they can show it to me that I can understand how I can do it)
2) How to attach the query to the button_click event
I know it should be something like this.. select colums from db where numbers=inputtextbox



Any idea or suggestion would be greatly appreciated

I have to do the same but backwards..aka adding data to the database but if I can get an idea on how i can so the above. then maybe I can figure out the rest


Andrea Montanar

Adding code to a visual basic 2005 button to run a sql query

by Andrea Montanar » Fri, 19 Nov 2010 07:05:45

thread moved to Data Bindings forum..

Rong-Chun Zhang - MSF

Adding code to a visual basic 2005 button to run a sql query

by Rong-Chun Zhang - MSF » Sun, 21 Nov 2010 08:06:46

Hi Marigb,

1) To bind the TextBox to data source, try something like this:

Code Snippet

Private dt As New DataTable()

Me.TextBox2.DataBindings.Add("Text", dt, "productId")

Me.TextBox3.DataBindings.Add("Text", dt, "Quantity")

2)How to attach the query to the button_click event Try somethinglike this:

Code Snippet

AddHandler Me.Button1.Click, AddressOf button1_Click

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim cm As New SqlCommand("Select * from [Order Details] where OrderId = @OrderId")

Dim da As New SqlDataAdapter()

cm.Parameters.Add("@OrderId", SqlDbType.Int)

If Me.TextBox1.Text <> "" Then

cm.Parameters(0).Value = Me.TextBox1.Text

Else

cm.Parameters(0).Value = "0000"

End If

cm.Connection = cn

da.SelectCommand = cm

dt.Clear()

da.Fill(dt)

Me.DataGridView1.DataSource = dt

End Sub

The whole code sample is shown below.

Code Snippet

Imports System.Data.SqlClient

Public Class Form7

Private dt As New DataTable()

Private cn As New SqlConnection("Server =Cael;Database = NorthWind; Integrated Security = SSPI;")

Private Sub Form7_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

Dim cm As New SqlCommand("Select * from [Order Details]")

Dim da As New SqlDataAdapter()

cm.Connection = cn

da.SelectCommand = cm

dt.Clear()

da.Fill(dt)

Me.DataGridView1.DataSource = dt

Me.TextBox2.DataBindings.Add("Text", dt, "productId")

Me.TextBox3.DataBindings.Add("Text", dt, "Quantity")

AddHandler Me.Button1.Click, AddressOf button1_Click

End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim cm As New SqlCommand("Select * from [Order Details] where OrderId = @OrderId")

Dim da As New SqlDataAdapter()

cm.Parameters.Add("@OrderId", SqlDbType.Int)

If Me.TextBox1.Text <> "" Then

cm.Parameters(0).Value = Me.TextBox1.Text

Else

cm.Parameters(0).Value = "0000"

End If

cm.Connection = cn

da.SelectCommand = cm

dt.Clear()

da.Fill(dt)

Me.DataGridView1.DataSource = dt

End Sub

End Class

Hope this helps.

Regards