Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm receiving an error when publishing my code. Please help me identify what the problem may be.

The error is on line 77: Total = Total + sdr.GetDecimal(3);


using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI;

public partial class Cart : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var skus = Convert.ToString(Session["DataTable"]).Split(',').ToList();

Session["ItemsInCart"] = skus.Count;

var multiSelect = new SqlCommand();

string cmdString1;

cmdString1 = "Select Manufacture, SKU, Name, Price From Products WHERE SKU = " + Session["CartTable"];

var connection =
new SqlConnection(
"Data Source=Omisbi3.niunt.niu.edu;Initial Catalog= 675_z1626585;User ID=OMIS675FA;Password=Omis.675!");

SqlDataReader sdr;

decimal Total = 0;

//Creating the table

var DT = new DataTable("CartDisplay");

var column1 = new DataColumn("SKU");

column1.DataType = Type.GetType("System.Int32");

var column2 = new DataColumn("Manufacture");

column2.DataType = Type.GetType("System.String");

var column3 = new DataColumn("Name");

column3.DataType = Type.GetType("System.String");

var column4 = new DataColumn("Price");

column4.DataType = Type.GetType("System.Decimal");

DT.Columns.Add(column1);

DT.Columns.Add(column2);

DT.Columns.Add(column3);

DT.Columns.Add(column4);

for (var i = 0; i <= skus.Count - 1; i++)

{
multiSelect.CommandText = "Select SKU, Manufacturer, Name, Price From Products WHERE SKU = @SKUS";
multiSelect.Parameters.Clear();
multiSelect.Parameters.AddWithValue("@skus", skus[i]);

multiSelect.Connection = connection;

multiSelect.Connection.Open();

sdr = multiSelect.ExecuteReader();

while (sdr.Read())
sdr = multiSelect.ExecuteReader();
{
DataRow Row1;

Row1 = DT.NewRow();

Total = Total + sdr.GetDecimal(3);

Row1["SKU"] = sdr.GetInt32(0);

Row1["Manufacturer"] = sdr.GetString(1);

Row1["Name"] = sdr.GetString(2);

Row1["Price"] = sdr.GetDecimal(3);

DT.Rows.Add(Row1);
}

multiSelect.Connection.Close();

lblResults.Text = "Your total is $" + Total;
}

GridView1.DataSource = DT;

GridView1.DataBind();
}
}

What I have tried:

I'm not sure what to do. I really need help ASAP. Thank you.
Posted
Updated 14-Dec-17 19:58pm

1 solution

Look at your code:
C#
sdr = multiSelect.ExecuteReader();

while (sdr.Read())
sdr = multiSelect.ExecuteReader();
Why are you calling ExecuteReader inside the Read loop?

In addition, Have a look at the SQL IN Operator[^]:
SQL
SELECT * FROM MyTable WHERE MyColumn IN (1, 3, 5, 7, 9)
 
Share this answer
 
Comments
Member 13576953 15-Dec-17 2:00am    
I'm new to coding. What should the code look like?
OriginalGriff 15-Dec-17 2:11am    
You are kidding, right?
The code you have is like buying a new car (Calling ExecuteReader), waiting for it to be delivered (Calling Read) so you can drive it, and immediately you want to go on any journey (enter the loop body) you throw the car away and buy a new one (call ExecuteReader). You then try to drive the car you just bought before it has been delivered!

Sit down, look at the code, and think about what it does. It's pretty obvious, and you will learn a lot better than if I just show you your own code with a line missing...
Member 13576953 15-Dec-17 2:21am    
Thank you so much. I figured it out. That was a mistake that I pasted twice (once in the wrong spot).
OriginalGriff 15-Dec-17 2:29am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900