2017-01-06 4 views
-2
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); 
    } 
} 

なかった、のDataGridViewの名前はitemgridある入力文字列がデータベースにDataGridViewのデータを挿入する正しいフォーマットC#のWinフォームアプリケーションで

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 = ""; 
} 
+0

日付形式 – Vijay

+0

をチェックすると、列の一部が文字列ではなく、すべてを文字列(varcharまたはnvarchar)として挿入しています。 – Laazo

+1

あなたはどのデータベースを使用していますか?あなたの「挿入」ステートメントが悪いです。 – jdweng

答えて

1

まず、私はこの中に挿入を実行するために動的SQLを構築助言ありませんこれはSQLインジェクション(https://en.wikipedia.org/wiki/SQL_injection)のリスクを引き起こします。しかし、あなたのコードは、非生産(すなわち、あなたは上記のリスクを気にしない)の代わりに、このパターンを試してみてください...

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

これは/デバッグを読むためにはるかに簡単パターンでされている場合。直ちにsInsertSQLを直接ペインに出力してから、SSMSにコピー/ペーストして何が間違っているのかを知ることができます。

+0

+1はString.Formatを使用しています。補間も機能します(サポートしているC#のそれ以降のバージョンを使用している場合)。 https://msdn.microsoft.com/en-us/library/dn961160.aspx –

関連する問題