C# 输入字符串的格式不正确#winform应用程序 private void btnsave\u单击(对象发送者,事件参数e) { 尝试 { 对于(int i=0;i

C# 输入字符串的格式不正确#winform应用程序 private void btnsave\u单击(对象发送者,事件参数e) { 尝试 { 对于(int i=0;i,c#,input,datagridview,C#,Input,Datagridview,将数据从datagridview插入数据库,datagridview的名称为itemgrid private void btnsave_Click(object sender, EventArgs e) { try { for (int i = 0; i < itemgrid.RowCount - 1; i++ ) { dru.insertdata("insert into tbl_godown (date,categ

将数据从datagridview插入数据库,datagridview的名称为itemgrid

private void btnsave_Click(object sender, EventArgs e)
{
    try
    {
        for (int i = 0; i < itemgrid.RowCount - 1; i++ )
        {
            dru.insertdata("insert into tbl_godown (date,category,product,quantity,MRP,salesrate,margin,Total,vendor,unit)values('" + itemgrid.Rows[i].Cells["Column1"].Value.ToString() + "','" + itemgrid.Rows[i].Cells["Column2"].Value.ToString() + "','" + itemgrid.Rows[i].Cells["Column3"].Value.ToString() + "','" + itemgrid.Rows[i].Cells["Column4"].Value.ToString() + "','" + double.Parse(itemgrid.Rows[i].Cells["Column5"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column6"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column7"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column8"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column9"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column10"].Value.ToString()) + "') ");
            MessageBox.Show("Insert Successfully");
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
private void txtquantity\u TextChanged(对象发送方,事件参数e)
{
mulfunc();
}
私有void txtrate_TextChanged(对象发送方,事件参数e)
{
mulfunc();
marginfunc();
}
私有void txtMRP_TextChanged(对象发送方,事件参数e)
{
marginfunc();
}
/// 
///利润计算(销售率-供应商费率=利润)
/// 
公共基金()
{
尝试
{
双价1;
双价2;
如果(!double.TryParse(txtMRP.Text,out val1)| |!double.TryParse(txtrate.Text,out val2))
返回;
双val3=val2-val1;
//在这里,您可以定义显示乘法结果的文本框
txtmargin.Text=val3.ToString();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
/// 
///数量*销售率
/// 
公共void mulfunc()
{
尝试
{
双价1;
双价2;
如果(!double.TryParse(txtquantity.Text,out val1)| |!double.TryParse(txtrate.Text,out val2))
返回;
双val3=val1*val2;
//在这里,您可以定义显示乘法结果的文本框
txttotal.Text=val3.ToString();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
公共空间清除()
{
cmbunit.Text=“”;
txtprod.Text=“”;
txtquantity.Text=“”;
txtMRP.Text=“”;
txtrate.Text=“”;
txtmargin.Text=“”;
txtotal.Text=“”;
txtvendor.Text=“”;
}

首先,我不建议以这种方式构建动态SQL来执行插入,因为这会带来SQL注入的风险()。但是,如果您的代码是非生产性的(即,您不关心上述风险),请尝试此模式

private void txtquantity_TextChanged(object sender, EventArgs e)
{
    mulfunc();
}

private void txtrate_TextChanged(object sender, EventArgs e)
{
        mulfunc();
        marginfunc();
}

private void txtMRP_TextChanged(object sender, EventArgs e)
{
        marginfunc();
}

/// <summary>
/// margin calculations(sales rate-vendor rate = margin)
/// </summary>
public void marginfunc()
{
    try
    {
        double val1;
        double val2;

        if (!double.TryParse(txtMRP.Text, out val1) || !double.TryParse(txtrate.Text, out val2))
                return;

        double val3 = val2 - val1;

        // Here you define what TextBox should show the multiplication result
        txtmargin.Text = val3.ToString();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

/// <summary>
/// quantity * sales rate
/// </summary>
public void mulfunc()
{
    try 
    {
        double val1;
        double val2;

        if (!double.TryParse(txtquantity.Text, out val1) || !double.TryParse(txtrate.Text, out val2))
                return;

        double val3 = val1 * val2;

        // Here you define what TextBox should show the multiplication result
        txttotal.Text = val3.ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

public void clear()
{
    cmbunit.Text = "";
    txtprod.Text = "";
    txtquantity.Text = "";
    txtMRP.Text = "";
    txtrate.Text = "";
    txtmargin.Text = "";
    txttotal.Text = "";
    txtvendor.Text = "";
}
{
对于(int i=0;i

这是一个更容易读取/调试的模式。您始终可以将sInsertSQL输出到即时窗格,然后复制/粘贴到SSMS中,以找出问题所在。

检查日期格式您正在将所有内容作为字符串(varchar或nvarchar)插入,而某些列不是字符串。您使用的是什么数据库?您的“insert”语句不正确。dateformat是:“yyyy mm dd”dru是什么类型的对象?+1用于使用String.Format。插值也可以工作(如果您使用支持它的更高版本的C#)。
        {
            for (int i = 0; i < itemgrid.RowCount - 1; i++ )
            {
                var sInsertSQL = string.Format(@"
insert tbl_godown (
    date,category,product,quantity,MRP,salesrate,margin,Total,vendor,unit
    )
    values(
        '{0}','{1}','{2}','{3}','{4}',{5},{6},{7},{8},{9}
        )'",
                    itemgrid.Rows[i].Cells["Column1"].Value,
                    itemgrid.Rows[i].Cells["Column2"].Value,
                    itemgrid.Rows[i].Cells["Column3"].Value,
                    itemgrid.Rows[i].Cells["Column4"].Value,
                    Convert.ToDouble(itemgrid.Rows[i].Cells["Column5"].Value),
                    Convert.ToDouble(itemgrid.Rows[i].Cells["Column6"].Value),
                    Convert.ToDouble(itemgrid.Rows[i].Cells["Column7"].Value),
                    Convert.ToDouble(itemgrid.Rows[i].Cells["Column8"].Value),
                    Convert.ToDouble(itemgrid.Rows[i].Cells["Column9"].Value),
                    Convert.ToDouble(itemgrid.Rows[i].Cells["Column10"].Value)
                    );
                dru.insertdata(sInsertSQL);
                MessageBox.Show("Insert Successfully");
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }