problem gettin specific record..
-
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
-
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?
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
-
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
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; }
-
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; }
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
-
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; }
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); }
-
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
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));
-
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));
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
forSelectedValue
?
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
-
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); }
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"];
-
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"];
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!! -
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
forSelectedValue
?
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