2014-03-26 27 views
0

我想插入在數據表中的數據網格視圖中輸入的值.. 我的代碼試圖..如何將值存儲到在網格視圖中輸入的數據表中?

private void btnSave_Click(object sender, EventArgs e) 
{ 
    billNO++; 
    if (con.State == ConnectionState.Open) { con.Close(); } 
    con.Open(); 
    string s = "CREATE TABLE [" + "" + combCustomerName.Text + "] (SlNO int Not Null , ItemDesc varchar(100) , ItemDetails varchar(100) , UMO varchar(10) , Quntity numeric(10,3) , Rate numeric(10,2) , Amount numeric(10,2) , GrossTot numeric(10,2) , Discount numeric(7,2) , Taxpc numeric(5,2) , TaxAmt numeric(5,2) , OtherAmt numeric(7,2) , NetAmt numeric(10,2))"; 
    SqlCommand cmd = new SqlCommand(s, con); 
    cmd.ExecuteNonQuery(); 
    //string insert=null; 
    SqlCommand inscmd = new SqlCommand(); 
    for (int i = 0; i < datagridItemEntry.Rows.Count; i++) 
    { 
     inscmd.CommandText = "INSERT INTO [" + "" + combCustomerName.Text + "] VALUES(" + datagridItemEntry.Rows[i].Cells[0].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[1].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[2].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[3].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[4].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[5].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[6].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[7].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[8].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[9].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[10].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[11].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[12].Value.ToString() + "','" + datagridItemEntry.Rows[i].Cells[13].Value.ToString() + "')"; 
    } 

    if (inscmd.ExecuteNonQuery() >= 1) 
    { 
     MessageBox.Show("Done!"); 
    } 
    con.Close(); 
} 

它創建表.. 我得到異常作爲指數超出範圍同時插入.. 幫助我。

+0

_「我試着用代碼..」_ ... _「幫我解決。」_我錯過了一些東西,比如你的代碼有問題。 –

+0

你應該解釋這個代碼面臨的問題。 – Zigma

+0

我想知道代碼將如何工作! 您每次單擊保存按鈕時都會創建表格! – Bharadwaj

回答

1

只需複製在執行第一個查詢後粘貼此代碼。

for (int i = 0; i < datagridItemEntry.Rows.Count; i++) 
       { 
        string query1 = "insert into "+combCustomerName.Text+" values(" + gvSalesInv.Rows[i].Cells[0].Value + "," + gvSalesInv.Rows[i].Cells[1].Value + "," + gvSalesInv.Rows[i].Cells[2].Value + "," + gvSalesInv.Rows[i].Cells[3].Value + "," + gvSalesInv.Rows[i].Cells[4].Value + "," + gvSalesInv.Rows[i].Cells[5].Value + "," + gvSalesInv.Rows[i].Cells[6].Value + "," + gvSalesInv.Rows[i].Cells[7].Value + "," + gvSalesInv.Rows[i].Cells[8].Value + "," + gvSalesInv.Rows[i].Cells[9].Value + "," + gvSalesInv.Rows[i].Cells[10].Value + "," + gvSalesInv.Rows[i].Cells[11].Value + ")"; 
        SqlCommand cmd1 = new SqlCommand(query1, con); 
        cmd1.ExecuteNonQuery(); 

       } 
相關問題