如何将带有Texbox文本的列表列相乘? [英] How Do I Multiply A List Column With A Texbox Text?

查看:53
本文介绍了如何将带有Texbox文本的列表列相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我查一下我的评论

  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( 选择ID,Item_Code,数量,库存中的商品,其中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());

// 请点击此处
item.SubItems.Add (listView1.Items [ 3 ]。ToString()* listView1.Items [ 2 ]。ToString() );
listView1.Items.Add(item);


}

解决方案

通过乘法字符串你有什么期望?如果这些字符串不是有效的数字表示怎么办?



所有算术方法都必须针对数字运行,而不是字符串。



提示:

<前lang =cs> 十进制数量,价格,总数;
if decimal .TryParse(listView1.Items [ 3 ]。ToString(), out 价格)&&& decimal 。 TryParse(listView1.Items [ 2 ]。ToString(), out quantity)){
总=价格*数量;
}
其他 {
// 价格或数量存在问题,这不是有效的数字代表
}


< blockquote>尝试 item.SubItems.Add(Convert.ToInt(listView1.Items [3])* Convert.ToInt(listView1.Items [2]));

更简洁的方法是使用 Int32.TryParse [ ^ ]以确保列表视图列中的值是整数,


一些事情:

1.切换到使用SQL的参数化查询以避免SQL注入问题的风险:

  private   void  addbtn_Click( object  sender,EventArgs e)
{
string connectionstring = @ 数据源= .;初始目录= SuperM;集成安全性=真;
SqlConnection sqlcon = new SqlConnection(connectionstring);
sqlcon.Open();

SqlCommand cm = new SqlCommand( 选择ID,Item_Code,数量,库存中的商品,其中item_code = @ICode,sqlcon);

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



2.正如其他人所说,你不能乘以字符串。这也带来了DB中数据类型的问题?我将假设价格列具有数字数据类型,如整数,单,双,货币。

  int 数量;  //  我假设这实际上是一个整数 
if (!int.TryParse(quantitytxt.Text, out 数量))
{
// 输入无效,您决定如何处理这种情况!
}
< span class =code-keyword> 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 [ 价格]); // 演员表可能已足够
item.SubItems.Add(price.ToString() );

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



上述代码尚未尝试过...


Please help me check where i comment

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);


           }

解决方案

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:

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
}


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,


A couple of things:
1. Switch to using a parameterized query for the SQL to avoid the risk of SQL injection issues:

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.

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...


这篇关于如何将带有Texbox文本的列表列相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆