Driving me crazy...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an employee table in a SQL Server database
I know how to bind data into a drop down list for a Field Name called State.

strSQL = "Select StateAbbreviation + ' - ' + StateName as StateInfo, StateAbbreviation From US_State"
objAdapter = New SqlDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "USState")
lstState.DataSource = objDataSet
lstState.DataMember = "USState"
'lstState.DataTextField = "StateInfo"
lstState.DataValueField = "StateInfo"
lstState.DataBind()

After I bind it, it shows 52 states in the drop down box...
but when I retrieve the data using dataset, how am i able to show TX on the drop down list instead of showing AK all the time

Also, how am i able to add a blank record for a drop down list since i don't want use have to choose a state...that means the drop down list showing 53 (52 states + 1 blank value)

Thanks... they have been drving me crazy for the whole day... VB6 is much easier...
 
Try something like this:

"Select '' as StateInfo, 'XX' as StateAbbreviation from US_State UNION
Select StateAbbreviation + ' - ' + StateName as StateInfo, StateAbbreviation
From US_State

'' = empty string of course.

To bind use:
lstState.DisplayMember = "StateInfo";
lstState.ValueMember = "StateAbbreviation";
lstState.DataSource = objDataSet.Tables[0];

To set the value to 'TX' you should be able to use:
lstState.SelectedValue = 'TX';

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

Ed said:
I have an employee table in a SQL Server database
I know how to bind data into a drop down list for a Field Name called State.

strSQL = "Select StateAbbreviation + ' - ' + StateName as
StateInfo, StateAbbreviation From US_State"
objAdapter = New SqlDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "USState")
lstState.DataSource = objDataSet
lstState.DataMember = "USState"
'lstState.DataTextField = "StateInfo"
lstState.DataValueField = "StateInfo"
lstState.DataBind()

After I bind it, it shows 52 states in the drop down box...
but when I retrieve the data using dataset, how am i able to show TX on
the drop down list instead of showing AK all the time
Also, how am i able to add a blank record for a drop down list since i
don't want use have to choose a state...that means the drop down list
showing 53 (52 states + 1 blank value)
Thanks... they have been drving me crazy for the whole day... VB6 is much
easier...
 
Thanks Ritch,
but when i run the code for displaymember and valuemember, error occurs and it said it is not a member of system.UI
Any idea?

Ed

C Addison Ritchie said:
Try something like this:

"Select '' as StateInfo, 'XX' as StateAbbreviation from US_State UNION
Select StateAbbreviation + ' - ' + StateName as StateInfo, StateAbbreviation
From US_State

'' = empty string of course.

To bind use:
lstState.DisplayMember = "StateInfo";
lstState.ValueMember = "StateAbbreviation";
lstState.DataSource = objDataSet.Tables[0];

To set the value to 'TX' you should be able to use:
lstState.SelectedValue = 'TX';

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

Ed said:
I have an employee table in a SQL Server database
I know how to bind data into a drop down list for a Field Name called State.

strSQL = "Select StateAbbreviation + ' - ' + StateName as
StateInfo, StateAbbreviation From US_State"
objAdapter = New SqlDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "USState")
lstState.DataSource = objDataSet
lstState.DataMember = "USState"
'lstState.DataTextField = "StateInfo"
lstState.DataValueField = "StateInfo"
lstState.DataBind()

After I bind it, it shows 52 states in the drop down box...
but when I retrieve the data using dataset, how am i able to show TX on
the drop down list instead of showing AK all the time
Also, how am i able to add a blank record for a drop down list since i
don't want use have to choose a state...that means the drop down list
showing 53 (52 states + 1 blank value)
Thanks... they have been drving me crazy for the whole day... VB6 is much
easier...
 
I just realized you are working in WebForms. The code in my last post is
for a dropdown box on a WinForms application. I will try to answer at a
later date. Sorry...

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

Ed said:
Thanks Ritch,
but when i run the code for displaymember and valuemember, error occurs
and it said it is not a member of system.UI
Any idea?

Ed

C Addison Ritchie said:
Try something like this:

"Select '' as StateInfo, 'XX' as StateAbbreviation from US_State UNION
Select StateAbbreviation + ' - ' + StateName as StateInfo, StateAbbreviation
From US_State

'' = empty string of course.

To bind use:
lstState.DisplayMember = "StateInfo";
lstState.ValueMember = "StateAbbreviation";
lstState.DataSource = objDataSet.Tables[0];

To set the value to 'TX' you should be able to use:
lstState.SelectedValue = 'TX';

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

Ed said:
I have an employee table in a SQL Server database
I know how to bind data into a drop down list for a Field Name called State.

strSQL = "Select StateAbbreviation + ' - ' + StateName as
StateInfo, StateAbbreviation From US_State"
objAdapter = New SqlDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "USState")
lstState.DataSource = objDataSet
lstState.DataMember = "USState"
'lstState.DataTextField = "StateInfo"
lstState.DataValueField = "StateInfo"
lstState.DataBind()

After I bind it, it shows 52 states in the drop down box...
but when I retrieve the data using dataset, how am i able to show TX o
n
the drop down list instead of showing AK all the time
Also, how am i able to add a blank record for a drop down list since i
don't want use have to choose a state...that means the drop down list
showing 53 (52 states + 1 blank value)
Thanks... they have been drving me crazy for the whole day... VB6 is
much
easier...
 
Ok try this:

"Select '' as StateInfo, 'XX' as StateAbbreviation from US_State UNION
Select StateAbbreviation + ' - ' + StateName as StateInfo, StateAbbreviation
From US_State

'' = empty string of course.

To bind use:
lstState.DataTextField = "StateInfo";
lstState.DataValueField = "StateAbbreviation";
lstState.DataSource = objDataSet.Tables[0];
lstState.DataBind();

To set the value to 'TX' you should be able to use:
lstState.SelectedValue = 'TX';

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

C Addison Ritchie said:
I just realized you are working in WebForms. The code in my last post is
for a dropdown box on a WinForms application. I will try to answer at a
later date. Sorry...

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

Ed said:
Thanks Ritch,
but when i run the code for displaymember and valuemember, error occurs
and it said it is not a member of system.UI
Any idea?

Ed

C Addison Ritchie said:
Try something like this:

"Select '' as StateInfo, 'XX' as StateAbbreviation from US_State UNION
Select StateAbbreviation + ' - ' + StateName as StateInfo, StateAbbreviation
From US_State

'' = empty string of course.

To bind use:
lstState.DisplayMember = "StateInfo";
lstState.ValueMember = "StateAbbreviation";
lstState.DataSource = objDataSet.Tables[0];

To set the value to 'TX' you should be able to use:
lstState.SelectedValue = 'TX';

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

I have an employee table in a SQL Server database
I know how to bind data into a drop down list for a Field Name called
State.

strSQL = "Select StateAbbreviation + ' - ' + StateName as
StateInfo, StateAbbreviation From US_State"
objAdapter = New SqlDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "USState")
lstState.DataSource = objDataSet
lstState.DataMember = "USState"
'lstState.DataTextField = "StateInfo"
lstState.DataValueField = "StateInfo"
lstState.DataBind()

After I bind it, it shows 52 states in the drop down box...
but when I retrieve the data using dataset, how am i able to show TX
o
is
 
Back
Top