Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

Simple Webform and SQL Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henrik - 04 Jul 2007 20:36 GMT
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
Tom John - 04 Jul 2007 23:01 GMT
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:Henrik@discussions.microsoft.com]
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
Henrik - 06 Jul 2007 06:46 GMT
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

> Henrik
>
[quoted text clipped - 86 lines]
>
> Henrik

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.