Real Problem

  • Thread starter Thread starter Big E
  • Start date Start date
B

Big E

Okay here is my code. I'm using ASP.Net and SQL Server 2000. When I run this
code the selecteditem.value is 1 the selecteditem.text is ALABAMA. This is
correct. But when I try and get the value once this procedure has run my
selecteditem.value is ALABAMA and the selecteditem.text is ALABAMA. It's not
keeping the selecteditem.value after I change the combobox selection.

Thanks.

Big E

Dim Portal As String
Dim dsState As DataSet
Dim cnPortal As SqlConnection
Dim daState As SqlDataAdapter
Dim dtState As DataTable = New DataTable
Portal = ConfigurationSettings.AppSettings("Portal.ConnectionString")
dsState = New DataSet
cnPortal = New SqlConnection(Portal)
daState = New SqlDataAdapter("SELECT * FROM tlkpState", cnPortal)
daState.Fill(dsState, "tlkpState")
dtState = dsState.Tables("tlkpState")
Dim i As Integer


For i = 0 To dtState.Rows.Count - 1


cboStateCompany.Items.Add(dtState.Rows(i)("StateCode"))

cboStateCompany.SelectedItem.Value = (dtState.Rows(i)("StateId"))

cboStateCompany.SelectedItem.Text = (dtState.Rows(i)("StateCode"))

Next

cnPortal.Close()
cnPortal = Nothing
 
check your binding, it looks like you are rebinding with JUST the state
passed to the DDL, not the ID & State.
 
I'm not positive why that's not working, however, you might want to consider
looking into binding your datatable to the combobox instead of adding each
item individually, it's easier, and avoids user error:

Dim Portal As String
Dim dsState As DataSet
Dim cnPortal As SqlConnection
Dim daState As SqlDataAdapter
Dim dtState As DataTable = New DataTable
Portal = ConfigurationSettings.AppSettings("Portal.ConnectionString")
dsState = New DataSet
cnPortal = New SqlConnection(Portal)
daState = New SqlDataAdapter("SELECT * FROM tlkpState", cnPortal)
daState.Fill(dsState, "tlkpState")
dtState = dsState.Tables("tlkpState")

cboStateCompany.DataSource = dtState
cboState.DataValueField= "StateId"
cboState.DataTextField = "StateCode"
cboStateCompany.DataBind()

cnPortal.Close()
scnPortal = Nothing

see if that fixes anything for you...
(also, if my syntax is incorrect, sorry, I'm a C# programmer... VB.Net is
not my forte)

HTH,
-Cliff
 
Thanks Cliff it works now.


Cliff Harris said:
I'm not positive why that's not working, however, you might want to consider
looking into binding your datatable to the combobox instead of adding each
item individually, it's easier, and avoids user error:

Dim Portal As String
Dim dsState As DataSet
Dim cnPortal As SqlConnection
Dim daState As SqlDataAdapter
Dim dtState As DataTable = New DataTable
Portal = ConfigurationSettings.AppSettings("Portal.ConnectionString")
dsState = New DataSet
cnPortal = New SqlConnection(Portal)
daState = New SqlDataAdapter("SELECT * FROM tlkpState", cnPortal)
daState.Fill(dsState, "tlkpState")
dtState = dsState.Tables("tlkpState")

cboStateCompany.DataSource = dtState
cboState.DataValueField= "StateId"
cboState.DataTextField = "StateCode"
cboStateCompany.DataBind()

cnPortal.Close()
scnPortal = Nothing

see if that fixes anything for you...
(also, if my syntax is incorrect, sorry, I'm a C# programmer... VB.Net is
not my forte)

HTH,
-Cliff
 
Back
Top