Click here to Skip to main content
15,877,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me check where i comment
C#
private void addbtn_Click(object sender, EventArgs e)
       {
           string connectionstring = @"Data Source=.;Initial Catalog=SuperM;Integrated Security=True";
           SqlConnection sqlcon = new SqlConnection(connectionstring);
           sqlcon.Open();

           SqlCommand cm = new SqlCommand("Select ID, Item_Code, quantity, price from stock where item_code = '" + itemcodetxt.Text + "'", sqlcon);

           SqlDataReader reader = cm.ExecuteReader();

           listView1.View = View.Details;
 while (reader.Read())
           {
               var item = new ListViewItem();
               item.Text = reader["ID"].ToString();
               item.SubItems.Add(reader["item_code"].ToString());
               item.SubItems.Add(quantitytxt.Text);
               item.SubItems.Add(reader["price"].ToString());

             //please check here
 item.SubItems.Add(listView1.Items[3].ToString() * listView1.Items[2].ToString());
               listView1.Items.Add(item);


           }
Posted
Updated 18-Nov-15 3:09am
v2
Comments
glen205 18-Nov-15 9:11am    
What error is the code throwing?

It looks like you're trying to multiply two strings together, this won't end well....

Cast (parse or TryParse) the items to a numeric datatype e.g. int otherwise you won't be able to perform multiplication...

What do you expect by multiplicating strings? What if these strings are not valid number representations.

All arithmetic methods must be run against numbers, not strings.

Tip:
C#
decimal quantity, price, total;
if (decimal.TryParse(listView1.Items[3].ToString(), out price) && decimal.TryParse(listView1.Items[2].ToString(), out quantity)) {
   total = price * quantity;
}
else {
   // There is a problem with either the price, or the quantity, which is not a valid number representation
}
 
Share this answer
 
Comments
Member 11673987 18-Nov-15 9:45am    
InvalidArgument=Value of '3' is not valid for 'index'.
Parameter name: index

that was the error message i received
phil.o 18-Nov-15 10:07am    
Then you have to debug your code and see where it fails.
Obviously the index is out of the array's bounds; that's a perfect job for a debugging session.
Try item.SubItems.Add(Convert.ToInt(listView1.Items[3]) * Convert.ToInt(listView1.Items[2]));
A neater way would be to use Int32.TryParse[^] to make sure that the values inside the list view columns are integers,
 
Share this answer
 
Comments
Member 11673987 18-Nov-15 9:29am    
The price is coming from database
BillWoodruff 18-Nov-15 10:42am    
try Sri Abhninav's suggestion and see if it works !
Abhinav S 18-Nov-15 11:12am    
Shukriya BillWoodJi.
A couple of things:
1. Switch to using a parameterized query for the SQL to avoid the risk of SQL injection issues:
C#
private void addbtn_Click(object sender, EventArgs e)
{
  string connectionstring = @"Data Source=.;Initial Catalog=SuperM;Integrated Security=True";
  SqlConnection sqlcon = new SqlConnection(connectionstring);
  sqlcon.Open();
 
  SqlCommand cm = new SqlCommand("Select ID, Item_Code, quantity, price from stock where item_code = @ICode", sqlcon);

  SqlParameter param = cm.Parameters.Add("@ICode", SqlDbType.NVarChar);
  param.Value = itemcodetxt.Text; 
  SqlDataReader reader = cm.ExecuteReader();

2. As others have said, you cannot multiply strings. This also brings the question of what are the data types in the DB? I'm going to assume that the price column has a numeric data type like Integer, Single, Double, Currency.
C#
int quantity; // I'm assuming this is actually an integer
if (!int.TryParse(quantitytxt.Text, out quantity))
{
  // the input is invalid, you decide how you want to handle this case!
}
while (reader.Read())
{
  var item = new ListViewItem();
  item.Text = reader["ID"].ToString();
  item.SubItems.Add(reader["item_code"].ToString());
  item.SubItems.Add(quantitytxt.Text);
  decimal price = Convert.ToDecimal(reader["price"]); // a cast might be sufficient
  item.SubItems.Add(price.ToString());

  item.SubItems.Add((quantity * price).ToString());
  listView1.Items.Add(item);
}

The above code has not been tried...
 
Share this answer
 
v4

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