Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dropdownlistbox issue....

Dropdownlistbox issue....

Scheduled Pinned Locked Moved ASP.NET
helpdatabasetutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tash18
    wrote on last edited by
    #1

    Hi guys, Actually im developing a web application on VS2003 framework 1.1, in this application i have several textboxes and one dropdown list box. Now the items in my dropdown listbox gets populated from the database table also i have an insert button on clicking it the datas in the textbox and also the selected item in the dropdown list box gets inserted into my database table. Here comes the issue now in my dropdown listbox say there are 2 items for example --> Tiger, Lion... since here tiger is the first element by default my listbox shows tiger may be from the dropdown list if i select lion it inserts only tiger.. in short my listbox doesnt identify my selected item.. The code is as follows:

    private void Button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    card = DropDownList1.SelectedItem.Text.ToString();
    name = TextBox34.Text;
    age = TextBox35.Text;
    gender = TextBox36.Text;
    con = new SqlConnection();
    con.ConnectionString = Session["sqlcon"].ToString();
    con.Open();
    cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "Insert into card_details values('"+Session["User_ID"]+"',getDate(),'"+name+"','"+age+"','"+gender+"','"+card+"')";
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    con.Close();
    }
    catch (Exception exp)
    {
    Label39.Visible = true;
    Label39.Text = "Exception occured review log file for details";
    ExceptionHandler.HandleException("Exception",exp);
    }
    finally
    {
    if (rdr != null)
    {
    rdr.Close();
    }
    if (con != null)
    {
    con.Close();
    con.Dispose();
    }
    }
    }

    I really dont know whats wrong in my code do plz help me.. Thanks & Regards, Tash

    N R 2 Replies Last reply
    0
    • T Tash18

      Hi guys, Actually im developing a web application on VS2003 framework 1.1, in this application i have several textboxes and one dropdown list box. Now the items in my dropdown listbox gets populated from the database table also i have an insert button on clicking it the datas in the textbox and also the selected item in the dropdown list box gets inserted into my database table. Here comes the issue now in my dropdown listbox say there are 2 items for example --> Tiger, Lion... since here tiger is the first element by default my listbox shows tiger may be from the dropdown list if i select lion it inserts only tiger.. in short my listbox doesnt identify my selected item.. The code is as follows:

      private void Button1_Click(object sender, System.EventArgs e)
      {
      try
      {
      card = DropDownList1.SelectedItem.Text.ToString();
      name = TextBox34.Text;
      age = TextBox35.Text;
      gender = TextBox36.Text;
      con = new SqlConnection();
      con.ConnectionString = Session["sqlcon"].ToString();
      con.Open();
      cmd = new SqlCommand();
      cmd.CommandType = CommandType.Text;
      cmd.CommandText = "Insert into card_details values('"+Session["User_ID"]+"',getDate(),'"+name+"','"+age+"','"+gender+"','"+card+"')";
      cmd.Connection = con;
      cmd.ExecuteNonQuery();
      cmd.Dispose();
      con.Close();
      }
      catch (Exception exp)
      {
      Label39.Visible = true;
      Label39.Text = "Exception occured review log file for details";
      ExceptionHandler.HandleException("Exception",exp);
      }
      finally
      {
      if (rdr != null)
      {
      rdr.Close();
      }
      if (con != null)
      {
      con.Close();
      con.Dispose();
      }
      }
      }

      I really dont know whats wrong in my code do plz help me.. Thanks & Regards, Tash

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      <> wrote:

      whats wrong in my code

      Plenty. name = TextBox34.Text; First of all you are accepting direct, unvalidated input from user controls that can and will expose your application to SQL inject attacks. con.ConnectionString = Session["sqlcon"].ToString(); Do not store the connection string in a Session variable. Another security hole and unnecessary. You don't even handle the case where it could be null. card = DropDownList1.SelectedItem.Text.ToString(); When the button is clicked, whatever the currently selected item in the dropdown is what will be used.


      I know the language. I've read a book. - _Madmatt

      T 1 Reply Last reply
      0
      • N Not Active

        <> wrote:

        whats wrong in my code

        Plenty. name = TextBox34.Text; First of all you are accepting direct, unvalidated input from user controls that can and will expose your application to SQL inject attacks. con.ConnectionString = Session["sqlcon"].ToString(); Do not store the connection string in a Session variable. Another security hole and unnecessary. You don't even handle the case where it could be null. card = DropDownList1.SelectedItem.Text.ToString(); When the button is clicked, whatever the currently selected item in the dropdown is what will be used.


        I know the language. I've read a book. - _Madmatt

        T Offline
        T Offline
        Tash18
        wrote on last edited by
        #3

        Mark Nischalke wrote:

        When the button is clicked, whatever the currently selected item in the dropdown is what will be used.

        what ever item i select from the list box only the 1st element in the listbox gets inserted... may be if i select 5th item in the list and click the insert button the first element in the list gets inserted... why does that happen.. im confused...

        N 1 Reply Last reply
        0
        • T Tash18

          Hi guys, Actually im developing a web application on VS2003 framework 1.1, in this application i have several textboxes and one dropdown list box. Now the items in my dropdown listbox gets populated from the database table also i have an insert button on clicking it the datas in the textbox and also the selected item in the dropdown list box gets inserted into my database table. Here comes the issue now in my dropdown listbox say there are 2 items for example --> Tiger, Lion... since here tiger is the first element by default my listbox shows tiger may be from the dropdown list if i select lion it inserts only tiger.. in short my listbox doesnt identify my selected item.. The code is as follows:

          private void Button1_Click(object sender, System.EventArgs e)
          {
          try
          {
          card = DropDownList1.SelectedItem.Text.ToString();
          name = TextBox34.Text;
          age = TextBox35.Text;
          gender = TextBox36.Text;
          con = new SqlConnection();
          con.ConnectionString = Session["sqlcon"].ToString();
          con.Open();
          cmd = new SqlCommand();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "Insert into card_details values('"+Session["User_ID"]+"',getDate(),'"+name+"','"+age+"','"+gender+"','"+card+"')";
          cmd.Connection = con;
          cmd.ExecuteNonQuery();
          cmd.Dispose();
          con.Close();
          }
          catch (Exception exp)
          {
          Label39.Visible = true;
          Label39.Text = "Exception occured review log file for details";
          ExceptionHandler.HandleException("Exception",exp);
          }
          finally
          {
          if (rdr != null)
          {
          rdr.Close();
          }
          if (con != null)
          {
          con.Close();
          con.Dispose();
          }
          }
          }

          I really dont know whats wrong in my code do plz help me.. Thanks & Regards, Tash

          R Offline
          R Offline
          R Giskard Reventlov
          wrote on last edited by
          #4

          It sounds like you are populating the listbox but not taking PostBack into account. Wherever it is you are binding the data surround the code with:

          if (!PostBack)
          {
          ... code to bind listbox to data.
          }

          otherwise every time you hit the button the listbox gets repopulated and you will always get the first item in the list as your value.

          Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells

          T 1 Reply Last reply
          0
          • T Tash18

            Mark Nischalke wrote:

            When the button is clicked, whatever the currently selected item in the dropdown is what will be used.

            what ever item i select from the list box only the 1st element in the listbox gets inserted... may be if i select 5th item in the list and click the insert button the first element in the list gets inserted... why does that happen.. im confused...

            N Offline
            N Offline
            nainakarri
            wrote on last edited by
            #5

            Is AutoPostBack property set to true?

            Naina

            T 1 Reply Last reply
            0
            • R R Giskard Reventlov

              It sounds like you are populating the listbox but not taking PostBack into account. Wherever it is you are binding the data surround the code with:

              if (!PostBack)
              {
              ... code to bind listbox to data.
              }

              otherwise every time you hit the button the listbox gets repopulated and you will always get the first item in the list as your value.

              Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells

              T Offline
              T Offline
              Tash18
              wrote on last edited by
              #6

              Thanx alot u solved my probs... but let me knw y we use

              digital man wrote:

              if (!PostBack) { ... code to bind listbox to data. }

              thanx and regards, Tash

              R 1 Reply Last reply
              0
              • N nainakarri

                Is AutoPostBack property set to true?

                Naina

                T Offline
                T Offline
                Tash18
                wrote on last edited by
                #7

                Thanx my issue was with postback.. thanx a lot...

                1 Reply Last reply
                0
                • T Tash18

                  Thanx alot u solved my probs... but let me knw y we use

                  digital man wrote:

                  if (!PostBack) { ... code to bind listbox to data. }

                  thanx and regards, Tash

                  R Offline
                  R Offline
                  R Giskard Reventlov
                  wrote on last edited by
                  #8

                  Simply put, this construct tells the page that it should only do whatever is inside the 'if' should this be the first time the page has been loaded.

                  Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups