473,425 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Drop-down box binding

Can somebody tell me what's wrong with this?

I am trying to bind a drop-down box to a table (states). Thanks,

//binding for the State drop-down

SqlConnection cStates = new SqlConnection
(ConfigurationSettings.AppSettings["SqlConnectionString"]);

cStates.Open();
string strTable = "GEM.states";
string strWhere = "";

strWhere = " WHERE stateid=" + lstState.SelectedItem.Value;

string strState = "SELECT statename, stateprefix as State FROM " +
strTable + strWhere + " ORDER BY statename";

adapter.Fill(ds,strTable);
lstState.DataSource = ds.Tables[strTable].DefaultView;
lstState.DataBind();
Jun 27 '06 #1
2 1176
Hi Antonio,

If lstState is a System.Web.UI.WebControls.DropDownList then you have to set
the lstState.DataTextField and lstState.DataValueField properties. In this
case you could also set the lstState.DataMember to the name of your table
and set the lstState.DataSource to reference the DataSet that contains the
table.

There are some things I must mention as well:

1. You don't have to bind directly to the DefaultView. The DefaultView will
be used by the DropDownList automatically for binding.

2. Wrap the code that uses your cStates connection, except for the Open()
method, in a Try...Finally block and call cStates.Close() in the finally
block to ensure that your connection is closed in the case that an Exception
is thrown.

3 Use a stored procedure or a parameterized query for your SQL. Right now
your code is blatantly open to SQL-injection attacks and you might gain
performance using a stored procedure.

4. Use a strong-Typed DataSet. In VS.NET use the server explorer to browse
to your database. In Vs.NET 2005 you can use the database explorer. Add a
new file to your project, selecting the DataSet type from the new file
dialog. Drag a table from either the server explorer or the database
explorer to your empty DataSet in the designer.

HTH

"Antonio" <An*****@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
Can somebody tell me what's wrong with this?

I am trying to bind a drop-down box to a table (states). Thanks,

//binding for the State drop-down

SqlConnection cStates = new SqlConnection
(ConfigurationSettings.AppSettings["SqlConnectionString"]);

cStates.Open();
string strTable = "GEM.states";
string strWhere = "";

strWhere = " WHERE stateid=" + lstState.SelectedItem.Value;

string strState = "SELECT statename, stateprefix as State FROM " +
strTable + strWhere + " ORDER BY statename";

adapter.Fill(ds,strTable);
lstState.DataSource = ds.Tables[strTable].DefaultView;
lstState.DataBind();

Jun 27 '06 #2
Thank you, Dave. I am new to .NET. I'll try it and hopefully, I won't post
it again..:)

Antonio

"Dave Sexton" wrote:
Hi Antonio,

If lstState is a System.Web.UI.WebControls.DropDownList then you have to set
the lstState.DataTextField and lstState.DataValueField properties. In this
case you could also set the lstState.DataMember to the name of your table
and set the lstState.DataSource to reference the DataSet that contains the
table.

There are some things I must mention as well:

1. You don't have to bind directly to the DefaultView. The DefaultView will
be used by the DropDownList automatically for binding.

2. Wrap the code that uses your cStates connection, except for the Open()
method, in a Try...Finally block and call cStates.Close() in the finally
block to ensure that your connection is closed in the case that an Exception
is thrown.

3 Use a stored procedure or a parameterized query for your SQL. Right now
your code is blatantly open to SQL-injection attacks and you might gain
performance using a stored procedure.

4. Use a strong-Typed DataSet. In VS.NET use the server explorer to browse
to your database. In Vs.NET 2005 you can use the database explorer. Add a
new file to your project, selecting the DataSet type from the new file
dialog. Drag a table from either the server explorer or the database
explorer to your empty DataSet in the designer.

HTH

"Antonio" <An*****@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
Can somebody tell me what's wrong with this?

I am trying to bind a drop-down box to a table (states). Thanks,

//binding for the State drop-down

SqlConnection cStates = new SqlConnection
(ConfigurationSettings.AppSettings["SqlConnectionString"]);

cStates.Open();
string strTable = "GEM.states";
string strWhere = "";

strWhere = " WHERE stateid=" + lstState.SelectedItem.Value;

string strState = "SELECT statename, stateprefix as State FROM " +
strTable + strWhere + " ORDER BY statename";

adapter.Fill(ds,strTable);
lstState.DataSource = ds.Tables[strTable].DefaultView;
lstState.DataBind();


Jun 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: riehe | last post by:
Dear all I want to drop a node from a nodegroup. Can someone confirm if its enough to redistribute a node group with a partition map which doesn't include the node to be dropped? I can't see...
1
by: Ian Dobson | last post by:
Hi, I need to drop a schema from a database but it has 400 tables in it. Is there an easy way to do it other than drop table schema1.table1 drop table schema1.table2 etc.. to 400 and then drop...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
6
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
1
by: Darren | last post by:
I'm trying to create a file using drag and drop. I want to be able to select a listview item drag it to the shell and create a file. Each icon in the listview represents a blob in a database. When...
15
by: uwcssa | last post by:
I try to drop a table as: I got: During SQL processing it returned: SQL0478N The object type "TABLE" cannot be dropped because there is an object "sch.SQL070515104729271", of type "FUNCTION",...
1
by: Thi | last post by:
Hi, I am trying to develop an application that allows the users to drag a few file(s) from a zip archive to a destination. My question is, how do i determine where the drop destination is...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.