Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
Hey all,

I know this is a long shot on a mac forum but I don't want to sign up to another forum just to ask a single question.

What it is is I have a web form that allows a company to make car bookings but what I would like it to do is when they select a car Make from a DropDownList, the Model DropDownList will populate itself from my database. I have figured out that I need to use a SqlDataReader and use a command etc.

Here is how far I have got so far.

Code:
string make = makeDrop.SelectedItem.Text.ToString();

        System.Data.SqlClient.SqlConnection conn =
            new System.Data.SqlClient.SqlConnection(
                ConfigurationManager.ConnectionStrings[
                "VehiclesConnection"].ConnectionString);

        conn.Open();

        SqlDataReader myReader = null;
        SqlCommand myCommand = new SqlCommand(
            "SELECT Model FROM Car WHERE Make = @make", conn);

        myCommand.Parameters.Add(
            new SqlParameter(
                "@make",
                System.Data.SqlDbType.VarChar, 50, "Make"));

        myCommand.Parameters["@make"].Value = make;

        myReader = myCommand.ExecuteReader();

        while (myReader.Read())
        {
            makeDrop.Items.Add(myReader["Model"].ToString());
        }

        conn.Close();

All help is appreciated as usual :).

Please though don't come on to this thread saying that I should use a asp forum, being honest I would rather ask here before asking over at a windows forum, which I don't want to do.

Thanks,

Stephen
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
This is good - you getting results from the DB okay? If you are, then the next step is to populate the Model drop-down with these results. You'll probably need to, at the very least, get an array of Strings from the results object to feed to the drop-down box.

It's been a while since I last used ASP.NET, so I apologize for the lack of actual code. :eek:
 

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
This is good - you getting results from the DB okay? If you are, then the next step is to populate the Model drop-down with these results. You'll probably need to, at the very least, get an array of Strings from the results object to feed to the drop-down box.

It's been a while since I last used ASP.NET, so I apologize for the lack of actual code. :eek:

Magically just after I read your reply suddenly the answer came :)

All i needed to add was:

Code:
modelDrop.DataFieldValue = "Model";
modelDrop.DataTextField = "Model";

Thanks for the help :)

Stephen
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
FYI As far as I know there is a limitation in ASP.NET that you can only link 2 dropDownList's like this.
 

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
FYI As far as I know there is a limitation in ASP.NET that you can only link 2 dropDownList's like this.

Thats good, I only need one :).

Another question though, I have two labels that recieve their texts from the database, using a select statement, what I would like to do is to get the text using the same connection, parameters etc. I can get it to assign one label but I am unsure how to get two different items values using the same command. Any help to get me started?

Thanks thought :)

Stephen
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Exactly the same as above for the models, except you'll only need to read one value of each name with MyReader["ParameterName"];

and then you should be able to just use SELECT parameter1, parameter2 from *; to get both values.
 

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
But the thing is labels do not have datasources so I cannot assign the data source to the reader like I did with the drop down box. Unless the data reader will store the info like ""firstField" "secondField"" am I correct. If so is there a way to split the data up?

Sorry if my question is absolutely wrong, I'm just learning asp.net (hating it) and am just trying to understand it a little better.

Thanks,

Stephen
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
I was assuming you'd just call the SQL in Page_Load in the code behind and then set the labels text value to the value you get from that.
 

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
Ok, I'm actually executing the code after a button click but what I am meaning is how, after the datareader is populated do I get the data out and into a string or an int?

Thanks,

Stephen
 

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
Sorry to dig up this old thread. But I am wondering is there anyway to create websites using Xcode?

Visual Studio is bugging me and would like to return to the mac as my main platform. I'm sure that this is extremly easy but just I have no luck a the minute so can't seem to find it.

Thanks,

Stephen
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.