private void BtnSave_Click(object sender, EventArgs e)
{
using (SqlConnection sqlConn = new SqlConnection("Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True")) //or variable to it
{
string sqlQuery = @"INSERT INTO cottonpurchase VALUES(@slipno, @purchasedate, @farmercode, @farmername, @villagename, @basicprice, @weight, @totalamountbasic, @premium, @totalamountpremium, @totalamountpaid, @yeildestimates)";
//NOTE: RENAME "MyTable" to your database table!!!!
using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
{
cmd.Parameters.Add("@slipno", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtSlipNo.Text)) ? int.Parse(TxtSlipNo.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@purchasedate", SqlDbType.DateTime).Value = monthCalendar1.SelectionStart; //date of selection!
cmd.Parameters.Add("@farmercode", SqlDbType.Int).Value = Convert.ToInt32(TxtFarmerCode.Text);
cmd.Parameters.Add("@farmername", SqlDbType.VarChar, 100).Value = TxtFarmerName.Text;
cmd.Parameters.Add("@villagename", SqlDbType.VarChar, 100).Value = TxtVillageName.Text;
cmd.Parameters.Add("@basicprice", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtBasicPrice.Text)) ? int.Parse(TxtBasicPrice.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@weight", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtWeight.Text)) ? int.Parse(TxtWeight.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@totalamountbasic", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountBasic.Text)) ? int.Parse(TxtTotalAmountBasic.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@premium", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtPremium.Text)) ? int.Parse(TxtPremium.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@totalamountpremium", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountPremium.Text)) ? int.Parse(TxtTotalAmountPremium.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@totalamountpaid", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountPaid.Text)) ? int.Parse(TxtTotalAmountPaid.Text) : (object)DBNull.Value;
cmd.Parameters.Add("@yeildestimates", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtYeildEstimates.Text)) ? int.Parse(TxtYeildEstimates.Text) : (object)DBNull.Value;
sqlConn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
當我執行此我得到一個錯誤,指出:值插入數據庫
輸入字符串的不正確的 格式。
cmd.Parameters.Add(「@ farmercode」, SqlDbType.Int).Value = int.Parse(TxtFarmerCode.Text);