dropdownlist fomr SQL data...

Don
Joined
5 Oct 2005
Posts
11,242
Location
Liverpool
Hi all...

Sorry about this... got another question for ya.. whats wrong with this..

string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
System.Data.SqlClient.SqlConnection sqlconnection = new System.Data.SqlClient.SqlConnection(connectionString);

string queryString = "SELECT MAX ([book].[PriKey]) FROM [book]";
System.Data.SqlClient.SqlCommand Mycommand = new System.Data.SqlClient.SqlCommand(queryString, sqlconnection);

System.Data.SqlClient.SqlDataReader dr;
dr = Mycommand.ExecuteReader(CommandBehavior.CloseConnection);

while (dr.Read())
{
DropDownList ModelNumber = (DropDownList)Form.FindControl("ModelNumber");
ModelNumber.Items.Add(new ListItem((String)dr["FirstName"] + dr["LastName"], ""));
}
dr.Close();

the error I'm getting is this...

ExecuteReader requires an open and available Connection. The connection's current state is closed.

Thanks again...

Stelly
 
Haircut said:
I think the error message pretty much says it all.

You have created a new connection, but you never open it.
Try calling the .Open() method on your connection once it is created.

Yer done that... still getting an error though...

Stelly
 
ok have this...

if (!Page.IsPostBack)
{
BookingList.DataSource = SQLQueries.GetNonApp();
BookingList.DataBind();
//SQLQueries.PopulateList();

}

string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
System.Data.SqlClient.SqlConnection sqlconnection = new System.Data.SqlClient.SqlConnection(connectionString);

string queryString = "SELECT ([Resource].[ModelNumber]) FROM ([Resource])";
System.Data.SqlClient.SqlCommand Mycommand = new System.Data.SqlClient.SqlCommand(queryString, sqlconnection);

System.Data.SqlClient.SqlDataReader dr;
Mycommand.Connection.Open();
dr = Mycommand.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
DropDownList ModelNumber = (DropDownList)form1.FindControl("ModelNumber");
ModelNumber.Items.Add((new ListItem((String)dr["FirstName"] + dr["LastName"], "")));
}
dr.Close();

getting this...

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ')'.

Source Error:


Line 24: System.Data.SqlClient.SqlDataReader dr;
Line 25: Mycommand.Connection.Open();
Line 26: dr = Mycommand.ExecuteReader(CommandBehavior.CloseConnection);
Line 27: while (dr.Read())
Line 28: {

Thanks a lot for the help mate

Stelly
 
yer worked it out.... missed an 's' off a table name...

now there is a problem with

DropDownList ModelNumber = (DropDownList)form1.FindControl("ModelNumber");
ModelNumber.Items.Add((new ListItem((String)dr["ModelNumber"])));

I was thinking with the dropdownlist is that its a child control of the FormView, it is not accessible directly. You have to locate the control by using FindControl() method.

but its now coming up with...

Object reference not set to an instance of an object.

ARGH!

Stelly
 
Dr_Evil said:
How about doing this in the front-end like this:

<asp:SqlDataSource ID="myDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:myCS %>" SelectCommand="SELECT ID, Name FROM vw_my_View"></asp:SqlDataSource>

<asp:DropDownList runat="server" ID="myDropDown" DataTextField="Name" DataValueField="ID" DataSourceID="myDataSource"></asp:DropDownList>

isn't this just much easier?


Would that work in a datagrid??

Stelly
 
Dr_Evil said:
it works fine in my gridview. You'll have to define TemplateFields, then an ItemTemplate and an EditItemTemplate. I place normal labels in the ItemTemplate, and have the dropdown in the EditItemTemplate.

are you using .Net 2.0 ?

Yer all I want to do is to populate a dropdownlist from an SQL table... is that too much to ask lol... any ideas?

Stelly
 
L33 said:
Your current method of defining the connectionstring in the web.config rather than directly in the code-behind file is the correct way of doing things, so keep with that :)

private string connectionString = WebConfigurationManager.ConnectionStrings["whatever"].ConnectionString

protected void Page_Load()
{
if (!this.IsPostBack)
fillListBox();
}

private void fillListBox()
{
lstModelNumber.Items.Clear();

string selectSQL = "SELECT MAX ([book].[PriKey]) FROM [book]";

SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["lastname"] + ", " + ["firstname"];
lstModelNumber.Items.Add(newItem);
}
reader.Close()
catch (Exception ex)
{
//do whatever you want........
}
}

but that doesnt solve the problem of the dropdownlist... right? :)

Stelly
 
Dr_Evil said:
Stelly,

trust me - I tried doing things this way first, then I found out you could create those type of controls much easier in using <asp: tags. You can still access them in your code-behind, as they are server-controls. What you are trying to do is the long way around....

1. Create an ASP:SQLDatasource, use your connectionstring stored in your web.config, as you are doing now

2. Create an ASP:Dropdown and set the DataSourceID to your SQLDatasource

that's all needed to get a drop-down list populated with values from a database.

Cheer mate... have already started :)

Stelly
 
Back
Top Bottom