自动乘法结果和连接数据库 [英] Multiplication result automatic and to connecting the database

查看:45
本文介绍了自动乘法结果和连接数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在textbox4中计算3个文本框值到自动结果,并且这些值也会插入到数据库中。

请帮我急.........



我的尝试:



private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)

{





foreach(DataGridViewRow dataGridView1.Rows中的行)

{

row.Cells [dataGridView1.Columns [total]。索引]。 Value =(Convert.ToDouble(row.Cells [dataGridView1.Columns [length]。Index] .Value)* Convert.ToDouble(row.Cells [dataGridView1.Columns [breadth]。Index] .Value)*转换.ToDouble(row.Cells [dataGridView1.Columns [height]。Index] .Value));



}







}



i want to calculate 3 text box values to automatic result in textbox4, and also this values are inserted to the database.
Please help me urgent.........

What I have tried:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{


foreach(DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns["total"].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns["length"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["breadth"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["height"].Index].Value));

}



}

推荐答案

对于初学者,不要使用Convert for any用户输入。用户犯错误,所以总是使用TryParse验证它:

For starters, don't use Convert for any user input. Users make mistakes, so always validate it using TryParse:
double length;
if (!double.TryParse(myTextBox.Text))
   {
   ... report problem to user ...
   return;
   }

然后通过参数化查询将乘法结果传递给SQL:

Then pass the result of your multiplication to SQL via a parameterised query:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1) VALUES (@C1)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.ExecuteNonQuery();
        }
    }


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           DataGridViewRow dvr = dataGridView1.Rows[e.RowIndex];
           if (dvr != null)
           {
               textBox1.Text = dvr.Cells["length"].Value.ToString();
               textBox2.Text = dvr.Cells["breadth"].Value.ToString();
               textBox3.Text = dvr.Cells["height"].Value.ToString();

               textBox4.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text) + Convert.ToInt32(textBox3.Text)).ToString();
           }
       }


这篇关于自动乘法结果和连接数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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