Simple Webform and SQL Question

G

Guest

Hi,

I am new to VB2005, but have used VB6 for a few years.

I have a simple web form in an asp.net project consisting of a DropDownList
and a TextBox.

I have managed to set a datasource for the DropDownList so that it will
autopopulate with a list of data when the page loads.

However I want the TextBox to populate with data based on the item selected
in the DropDownList.

I can make a pretty simple SQL Query that will return the data I need,
here's a pseudo example of what I need:

TextBox1.Text = result of (SELECT comment from TableName WHERE ColumnName =
DropDownList.text)

What VB Code do I need on the SelectedIndexChanged event for the
DropDownList in order to populate the TextBox with the result of the simple
SQL Query ?

Best Regards,

Henrik
 
T

Tom John

Henrik

Here's a simple bit of CodeBehind that i think illustrates what you're
trying to do (assumes you have a webform with a DropDownList and TextBox
on it):

Imports System.Data
Imports System.Data.SqlClient

Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then
' Just to get the date in there quick and dirty
DropDownList1.Items.Add(New ListItem("Item 1", "1"))
DropDownList1.Items.Add(New ListItem("Item 2", "2"))
DropDownList1.Items.Add(New ListItem("Item 3", "3"))
End If

' Ensures the dropdown list causes a postback when changed
DropDownList1.AutoPostBack = True

End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

' Should really use the connection string from the web.config
here...
Using connection As New SqlConnection("Data Source=(local);Initial
Catalog=Test;Integrated Security=True;")
connection.Open()
Using command As New SqlCommand("SELECT Text FROM Table1 WHERE
ID = @id", connection)
command.Parameters.Add(New SqlParameter("@id",
DropDownList1.SelectedValue))
TextBox1.Text = command.ExecuteScalar().ToString
End Using
End Using

End Sub

End Class

Hope this gets you started

Cheers

Tom

-----Original Message-----
From: Henrik [mailto:[email protected]]
Posted At: 04 July 2007 20:36
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Simple Webform and SQL Question
Subject: Simple Webform and SQL Question

Hi,

I am new to VB2005, but have used VB6 for a few years.

I have a simple web form in an asp.net project consisting of a
DropDownList
and a TextBox.

I have managed to set a datasource for the DropDownList so that it will
autopopulate with a list of data when the page loads.

However I want the TextBox to populate with data based on the item
selected
in the DropDownList.

I can make a pretty simple SQL Query that will return the data I need,
here's a pseudo example of what I need:

TextBox1.Text = result of (SELECT comment from TableName WHERE ColumnName
=
DropDownList.text)

What VB Code do I need on the SelectedIndexChanged event for the
DropDownList in order to populate the TextBox with the result of the
simple
SQL Query ?

Best Regards,

Henrik
 
G

Guest

Thanks a lot. Your code was just what I needed to complete this small
project. Thanks for a clear and productive answer.

It's amazing how much easier some things have become with .net compared to
asp/vb classic.

Best Regards,

Henrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top