Dropdownlist Fill using C#

Last post 02-26-2008 5:22 PM by onSlaught. 2 replies.

Sort Posts:

  • Dropdownlist Fill using C#

    02-26-2008, 4:24 PM
    • Member
      28 point Member
    • jhaddington
    • Member since 01-30-2008, 6:57 PM
    • Posts 81

    Hey Guys,

    I am trying to fill a dropdownlist using the code behind approch (C#).  My code below is returning an error in the browser about null values.  Can anyone look over my code and see where I am going wrong please?!  Im new to asp and so am a little confused!!!

    Thanks in advance =)

    C# Code:

    Date =
    "default";

    CallNo = "default";

    myConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\inetpub\\wwwroot\\HOF\\App_Data\\HOF.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;";

    Date = myCalendar.TodaysDate.ToShortDateString();

    Username = (
    string)Session["UserName"];this.DataLoad();

    }

    private void DataLoad()

    {

    if (Session["UserName"] == null)

    {

    SqlConnection myConnection = new SqlConnection(myConnectionString);

    SqlCommand mycommand = new SqlCommand("SELECT KnowledgeID, FixName FROM Knowledge WHERE Location LIKE '%.txt%' AND SAPPS = 'False'", myConnection);SqlDataAdapter myDataAdapter = new SqlDataAdapter(mycommand);

    myConnection.Open();

    myDataAdapter.Fill(myDataSet,
    "Knowledge");

    DropDownList1.DataSource = myDataSet;

    DropDownList1.DataBind();

    }

  • Re: Dropdownlist Fill using C#

    02-26-2008, 4:40 PM
    Answer
    • All-Star
      90,223 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, Philippines
    • Posts 13,620
    • TrustedFriends-MVPs

    I think you forgot the line below after this line SqlCommand mycommand = new SqlCommand("SELECT KnowledgeID, FixName FROM Knowledge WHERE Location LIKE '%.txt%' AND SAPPS = 'False'", myConnection)

    mycommand .CommandType = CommandType.Text;
    mycommand .ExecuteNonQuery();

     

    Best Regards,
    Vincent Maverick Durano
    SDE|Microsoft MVP - ASP/ASP.NET

    "Code,Beer and Music ~ my way of being a programmer"
  • Re: Dropdownlist Fill using C#

    02-26-2008, 5:22 PM
    Answer
    • Contributor
      2,224 point Contributor
    • onSlaught
    • Member since 08-21-2007, 1:17 PM
    • a Phoenician
    • Posts 373

    Also make sure what you are pulling from the database does not have NULL values in them. You can filter them out in your WHERE clause if you do have them.

    Please mark as "answer" if this helped you in anyway.
Page 1 of 1 (3 items)