good day, As this is my first .Net prj., pls excuse my ignorance. I need to fill a textbox according to the value the user clicks in a dropdownlist box. the dropdownlist box is filled via a dataview. asp.net & sql 2000
Ex.
dropdownlist : (user chooses) 'AUSTIN'
(then according to the City)
textbox1: 'TEXAS'
THE DATA SIDE
------------------
I have 2 tables called StateMaster & CityMaster
the StateMaster table has the fields the CityMaster table has the fields
mst_StateCode (char, 2)(PK) mst_StateCode (char, 2)(FK)
mst_StateName (varchar, 50) mst_CityCode (varchar, 10)(PK)
mst_StateStatus (char, 1) mst_CityName (varchar, 50)
THE FORM SIDE
------------------
ddlcity1
textbox1
The plan basically is to fill in the textbox with the state name based on the selection in the ddlcity1 dropdownlist.
HERE IS THE CODE
----------------------
Private dvProducts As DataView
Private dvState As DataView
Public strMsg As String
Public strErrorMsg As String
Protected Const SQL_CONNECTION_STRING As String = _
"Server=SQLSERVER;Initial Catalog=database;UID=tempu
ser;passwo
rd=tempuse
r"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim scnnNW As New SqlConnection(SQL_CONNECTI
ON_STRING)
Dim strSQL As String = _
"SELECT * FROM CityMaster WHERE mst_CityStatus=0 order by mst_CityCode"
Dim scmd As New SqlCommand(strSQL, scnnNW)
' A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
Dim sda As New SqlDataAdapter(scmd)
' Create and Fill a new DataSet.
Dim ds As New DataSet
sda.Fill(ds)
If Not IsNothing(Cache("dvProduct
s")) Then
' All items in the cache are of type Object, so you have to explicitly
' cast it before working with it further.
dvProducts = CType(Cache("dvProducts"),
DataView)
Else ' DataView was not found in the cache.
' Create a new DataView.
dvProducts = ds.Tables(0).DefaultView
' Put the new DataView into the Cache
Cache("dvProducts") = dvProducts
End If
With ddlCity1
.DataSource = dvProducts
.DataValueField = "mst_StateCode"
.DataTextField = "mst_CityName"
.DataBind()
End With
ddlCity1.Items.Insert(0, New ListItem("--------Select One--------", "0"))
strSQL = "SELECT * FROM StateMaster WHERE mst_StateStatus=0 order by mst_StateCode"
Dim scmd1 As New SqlCommand(strSQL, scnnNW)
Dim sda1 As New SqlDataAdapter(scmd1)
' Create and Fill a new DataSet.
Dim ds1 As New DataSet
sda1.Fill(ds1)
If Not IsNothing(Cache("dvState")
) Then
dvState = CType(Cache("dvState"), DataView)
Else
dvState = ds.Tables(0).DefaultView
Cache("dvState") = dvState
End If
End Sub
Private Sub ddlCity1_SelectedIndexChan
ged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCity1.SelectedIndexChan
ged
Dim i As Integer
dvState.Sort = "mst_StateCode"
TextBox1.Text = ddlCity1.SelectedItem.Valu
e
i = dvState.Find(ddlCity1.Sele
ctedValue)
TextBox1.Text = CStr(dvState.Item(i)("mst_
StateName"
))
End Sub
but it dosent work. When i select a value in the dropdownlist, i get the value of the first item in the ddlcity1 - even though I've selected some other value.
Pls help.