Long shot asp.net

Discussion in 'Mac Programming' started by SRossi, Nov 30, 2009.

  1. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #1
    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
     
  2. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    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:
     
  3. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #3
    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
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    FYI As far as I know there is a limitation in ASP.NET that you can only link 2 dropDownList's like this.
     
  5. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #5
    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
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    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.
     
  7. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #7
    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
     
  8. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    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.
     
  9. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #9
    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
     
  10. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #10
    Code:
    Mylabel.Text = myReader["Label"].ToString();
    
    :)
     
  11. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #11
    Thanks so much a had thought it was like that, but had been giving me an error because I had misspelled my query :eek:

    Thanks for your help though :)

    Stephen
     
  12. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #12
    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
     

Share This Page