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. Database & SysAdmin
  3. Database
  4. problem gettin specific record..

problem gettin specific record..

Scheduled Pinned Locked Moved Database
help
12 Posts 3 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.
  • R Offline
    R Offline
    rcwoods
    wrote on last edited by
    #1

    hi Could someone plz help me! I am getting an "IndexOutOfRangeException not handled by user", in my code when im tryin to get a specific record, selected via a dropdownlist, and show the fields in textboxes. my code is like this: reader.Read(); ShipperDetails shipper = new ShipperDetails((int)reader["ShipperID"], (string)reader["CompanyName"], (string)reader["Phone"]); reader.Close(); this is where the error comes up. Please help..

    C 1 Reply Last reply
    0
    • R rcwoods

      hi Could someone plz help me! I am getting an "IndexOutOfRangeException not handled by user", in my code when im tryin to get a specific record, selected via a dropdownlist, and show the fields in textboxes. my code is like this: reader.Read(); ShipperDetails shipper = new ShipperDetails((int)reader["ShipperID"], (string)reader["CompanyName"], (string)reader["Phone"]); reader.Close(); this is where the error comes up. Please help..

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You have supplied a column name that does not exist. Also, it is better for debugging if you expand your code like this, because then you will have a better idea about exactly which item failed.

      int shipperId = (int)reader["ShipperID"];
      string companyName = (string)reader["CompanyName"];
      string phone = (string)reader["Phone"];
      ShipperDetails shipper = new ShipperDetails(shipperId,
      companyName, phone);


      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      R 1 Reply Last reply
      0
      • C Colin Angus Mackay

        You have supplied a column name that does not exist. Also, it is better for debugging if you expand your code like this, because then you will have a better idea about exactly which item failed.

        int shipperId = (int)reader["ShipperID"];
        string companyName = (string)reader["CompanyName"];
        string phone = (string)reader["Phone"];
        ShipperDetails shipper = new ShipperDetails(shipperId,
        companyName, phone);


        Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        R Offline
        R Offline
        rcwoods
        wrote on last edited by
        #3

        thanks for the reply. it says im getting an error with the ShipperID. I used an arraylist to store the objects. the problem is that its returning a shipperId = 0 when it reads the first row. and thats why its IndexoutOfRange. How can i fix this?

        C 1 Reply Last reply
        0
        • R rcwoods

          thanks for the reply. it says im getting an error with the ShipperID. I used an arraylist to store the objects. the problem is that its returning a shipperId = 0 when it reads the first row. and thats why its IndexoutOfRange. How can i fix this?

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          rcwoods wrote:

          the problem is that its returning a shipperId = 0 when it reads the first row

          From the code that you showed returning a shipperId of zero will not produce the exception you are getting. Is there any code missing?


          Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

          R 1 Reply Last reply
          0
          • C Colin Angus Mackay

            rcwoods wrote:

            the problem is that its returning a shipperId = 0 when it reads the first row

            From the code that you showed returning a shipperId of zero will not produce the exception you are getting. Is there any code missing?


            Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

            R Offline
            R Offline
            rcwoods
            wrote on last edited by
            #5

            here is more of my code maybe you can see whats wrong. this is from my web form: ShipperDetails[] shipperArray = dac.GetAllShippers(); foreach (ShipperDetails shipper in shipperArray) { ListItem item = new ListItem(); item.Text = shipper.CompanyName; item.Value = shipper.ID.ToString(); DropDownList1.Items.Add(item); } public ShipperDetails[] GetAllShippers() { SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("GetAllShippers", con); cmd.CommandType = CommandType.StoredProcedure; //collection for all shipper records ArrayList shippers = new ArrayList(); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ShipperDetails shipper = new ShipperDetails((int)reader["ShipperID"], (string)reader["CompanyName"], (string)reader["Phone"]); shippers.Add(shipper); } reader.Close(); return (ShipperDetails[])shippers.ToArray(typeof(ShipperDetails)); } this is called when i select a particular shipper from the dropDownList: public ShipperDetails GetShipper(int shipperID) { SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("GetShipper", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ShipperID", SqlDbType.Int, 4)); cmd.Parameters["@ShipperID"].Value = shipperID; try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow); //get 1st row reader.Read(); int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone); reader.Close(); return shipper; }

            C S 2 Replies Last reply
            0
            • R rcwoods

              here is more of my code maybe you can see whats wrong. this is from my web form: ShipperDetails[] shipperArray = dac.GetAllShippers(); foreach (ShipperDetails shipper in shipperArray) { ListItem item = new ListItem(); item.Text = shipper.CompanyName; item.Value = shipper.ID.ToString(); DropDownList1.Items.Add(item); } public ShipperDetails[] GetAllShippers() { SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("GetAllShippers", con); cmd.CommandType = CommandType.StoredProcedure; //collection for all shipper records ArrayList shippers = new ArrayList(); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ShipperDetails shipper = new ShipperDetails((int)reader["ShipperID"], (string)reader["CompanyName"], (string)reader["Phone"]); shippers.Add(shipper); } reader.Close(); return (ShipperDetails[])shippers.ToArray(typeof(ShipperDetails)); } this is called when i select a particular shipper from the dropDownList: public ShipperDetails GetShipper(int shipperID) { SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("GetShipper", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ShipperID", SqlDbType.Int, 4)); cmd.Parameters["@ShipperID"].Value = shipperID; try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow); //get 1st row reader.Read(); int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone); reader.Close(); return shipper; }

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              In GetShipper(int shipperID), does the reader actually return anything? (i.e. reader.HasRows == true) If you expect shipperID to be non-zero then perhaps the code that gets the shipperID out of the drop down isn't working properly...


              Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

              R 1 Reply Last reply
              0
              • R rcwoods

                here is more of my code maybe you can see whats wrong. this is from my web form: ShipperDetails[] shipperArray = dac.GetAllShippers(); foreach (ShipperDetails shipper in shipperArray) { ListItem item = new ListItem(); item.Text = shipper.CompanyName; item.Value = shipper.ID.ToString(); DropDownList1.Items.Add(item); } public ShipperDetails[] GetAllShippers() { SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("GetAllShippers", con); cmd.CommandType = CommandType.StoredProcedure; //collection for all shipper records ArrayList shippers = new ArrayList(); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ShipperDetails shipper = new ShipperDetails((int)reader["ShipperID"], (string)reader["CompanyName"], (string)reader["Phone"]); shippers.Add(shipper); } reader.Close(); return (ShipperDetails[])shippers.ToArray(typeof(ShipperDetails)); } this is called when i select a particular shipper from the dropDownList: public ShipperDetails GetShipper(int shipperID) { SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("GetShipper", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ShipperID", SqlDbType.Int, 4)); cmd.Parameters["@ShipperID"].Value = shipperID; try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow); //get 1st row reader.Read(); int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone); reader.Close(); return shipper; }

                S Offline
                S Offline
                sam
                wrote on last edited by
                #7

                check your StoredProcedure may be problem is there i.e. it is not returning the columns that you want to access

                rcwoods wrote:

                reader.Read(); int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone);

                and write this code like this if(reader.Read()) { int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone); }

                R 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  In GetShipper(int shipperID), does the reader actually return anything? (i.e. reader.HasRows == true) If you expect shipperID to be non-zero then perhaps the code that gets the shipperID out of the drop down isn't working properly...


                  Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                  R Offline
                  R Offline
                  rcwoods
                  wrote on last edited by
                  #8

                  reader did return true for having rows. this is how i get the shipperId out of the dropDownlist: protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { ShipperDetails shipper = dac.GetShipper(Convert.ToInt16(DropDownList1.SelectedItem.Value));

                  C 1 Reply Last reply
                  0
                  • R rcwoods

                    reader did return true for having rows. this is how i get the shipperId out of the dropDownlist: protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { ShipperDetails shipper = dac.GetShipper(Convert.ToInt16(DropDownList1.SelectedItem.Value));

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    You are shoving too much into one line of code. Separate out the method and property calls into separate lines:

                    ListItem selectedItem = DropDownList1.SelectedItem;
                    string dropValue = selectedItem.Value;
                    int shipperId = Convert.ToInt16(dropValue);
                    ShipperDetails shipper = dac.GetShipper(shipperId);

                    Now, you can step through the above and see what is actually happening. Incidentally, what happens if you change SelectedItem for SelectedValue?


                    Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                    R 1 Reply Last reply
                    0
                    • S sam

                      check your StoredProcedure may be problem is there i.e. it is not returning the columns that you want to access

                      rcwoods wrote:

                      reader.Read(); int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone);

                      and write this code like this if(reader.Read()) { int shipperId = (int)reader["ShipperID"]; string companyName = (string)reader["CompanyName"]; string phone = (string)reader["Phone"]; ShipperDetails shipper = new ShipperDetails(shipperId,companyName,phone); }

                      R Offline
                      R Offline
                      rcwoods
                      wrote on last edited by
                      #10

                      this is my stored procedure: CREATE PROCEDURE GetShipper @ShipperID int AS SELECT ShipperID, CompanyName, Phone FROM Shippers WHERE ShipperID = @ShipperID RETURN tried the code you said but its still giving same problem. IndexOutOfRange by: int shipperId = (int)reader["ShipperID"];

                      R 1 Reply Last reply
                      0
                      • R rcwoods

                        this is my stored procedure: CREATE PROCEDURE GetShipper @ShipperID int AS SELECT ShipperID, CompanyName, Phone FROM Shippers WHERE ShipperID = @ShipperID RETURN tried the code you said but its still giving same problem. IndexOutOfRange by: int shipperId = (int)reader["ShipperID"];

                        R Offline
                        R Offline
                        rcwoods
                        wrote on last edited by
                        #11

                        yes! you were right it was my stored procedure. My stored procedure looked like this before: CREATE PROCEDURE GetShipper @ShipperID int AS SELECT CompanyName, Phone FROM Shippers WHERE ShipperID = @ShipperID RETURN I was missing the ShipperID from my SELECT. Thanks So much for your time!!

                        1 Reply Last reply
                        0
                        • C Colin Angus Mackay

                          You are shoving too much into one line of code. Separate out the method and property calls into separate lines:

                          ListItem selectedItem = DropDownList1.SelectedItem;
                          string dropValue = selectedItem.Value;
                          int shipperId = Convert.ToInt16(dropValue);
                          ShipperDetails shipper = dac.GetShipper(shipperId);

                          Now, you can step through the above and see what is actually happening. Incidentally, what happens if you change SelectedItem for SelectedValue?


                          Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                          R Offline
                          R Offline
                          rcwoods
                          wrote on last edited by
                          #12

                          i got it right! turns out the error was in my stored procedure. Thanks for the help! much appreciated!

                          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