2016-04-12 11 views
0

Kullanıcı ödemeyi nakit olarak seçtiyse ve erişim tarihi maskelenmiş metin kullanılarak boş olarak eklenecekse, erişim veritabanına boş tarih eklemek istiyorum, hata ayıklayıcısını da kullanıyorum ancak hata ayıklayıcısını başka bir yere taşıyorum koşul ve onun bana burada veri uyuşmazlığı istisna veren bunun için im senin metin kutusu denetiminde girilen geçerli bir tarih olup olmadığınıerişim için boş tarih ekleme nasıl

string bank = txtbankname.Text; 
      bank = ""; 
      string cheque = txtchequeno.Text; 
      cheque = ""; 


      string billno = txtbillno.Text; 
      billno = ""; 


      string codecreate = txtcodecreator.Text; 
      codecreate = ""; 

      string connetionString = null; 
      connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString; 
      cnn.ConnectionString = connetionString; 

      string SqlString = "Insert Into Billing([FormNo],[Date],[TruckNo],[Office_Code],[Party_Code],[Party_Code1],[Location],[Supplier],[Item],[Invoice_no],[Invoice_date],[Package],[Weight],[Invest_Amount],[Percentage],[Amount],[Total_Amount],[Payment_Amount],[Payment_Type],[Bank_Name],[Cheque_No],[Cheque_Date],[Bill_No],[Bill_Date],[Code_Create]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
      using (cmd = new OleDbCommand(SqlString, cnn)) 
      { 
       cnn.Open(); 
       cmd.CommandType = CommandType.Text; 
       cmd.Parameters.AddWithValue("@FormNo", txtformno.Text); 
       cmd.Parameters.AddWithValue("@Date", txtdate.Text); 
       cmd.Parameters.AddWithValue("@TruckNo", txttruck.Text); 
       cmd.Parameters.AddWithValue("@Office_Code", txtofficecode.Text); 
       cmd.Parameters.AddWithValue("@Party_Code", txtpartycode.Text); 
       cmd.Parameters.AddWithValue("@Party_Code1", txtpartycode1.Text); 
       cmd.Parameters.AddWithValue("@Location", txtlocation.Text); 
       cmd.Parameters.AddWithValue("@Supplier", txtsupplier.Text); 
       cmd.Parameters.AddWithValue("@Item", txtitem.Text); 
       cmd.Parameters.AddWithValue("@Invoice_no", txtinvoice.Text); 
       cmd.Parameters.AddWithValue("@Invoice_date", DateTime.Parse(txtmaskinvoice.Text)); 
       cmd.Parameters.AddWithValue("@Package", txtpackage.Text); 
       cmd.Parameters.AddWithValue("@Weight", txtwieght.Text); 
       cmd.Parameters.AddWithValue("@Invest_Amount", Convert.ToDouble(txtinvestamount.Text)); 
       cmd.Parameters.AddWithValue("@Percentage", txtpercentage.Text); 
       cmd.Parameters.AddWithValue("@Amount", Convert.ToDouble(txtamount.Text)); 
       cmd.Parameters.AddWithValue("@Total_Amount", Convert.ToDouble(txttotalamount.Text)); 
       cmd.Parameters.AddWithValue("@Payment_Amount", Convert.ToDouble(txtpaymentamount.Text)); 
       cmd.Parameters.AddWithValue("@Payment_Type", txtpaymenttype.Text); 
       if (txtbankname.Text == "") 
       { 
        cmd.Parameters.AddWithValue("@Bank_Name", bank); 

       } 

       else 
       { 
        cmd.Parameters.AddWithValue("@Bank_Name", txtbankname.Text); 
       } 


       if (txtchequeno.Text == "") 
       { 
        cmd.Parameters.AddWithValue("@Cheque_No", cheque); 
       } 
       else 
       { 

        cmd.Parameters.AddWithValue("@Cheque_No", txtchequeno.Text); 
       } 


       DateTime chequeDate; 
       var value = (object)DBNull.Value; 
       if (DateTime.TryParseExact(txtmaskchequedate.Text,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out chequeDate)) 
       { 



        value = chequeDate; 
        cmd.Parameters.AddWithValue("@Cheque_Date", value); 

       } 
       else 
       { 

        cmd.Parameters.AddWithValue("@Cheque_Date",(txtmaskchequedate.Text)); 

       } 

       if (txtbillno.Text == "") 
       { 
        cmd.Parameters.AddWithValue("@Bill_No", billno); 

       } 
       else 
       { 
        cmd.Parameters.AddWithValue("@Bill_No", txtbillno.Text); 


       } 

       DateTime BillDate; 
       var value1 = (object)DBNull.Value; 
       if (DateTime.TryParseExact(txtmaskbilldate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out BillDate)) 
       { 



        value1 = BillDate; 
        cmd.Parameters.AddWithValue("@Bill_Date", value1); 

       } 
       else 
       { 

        cmd.Parameters.AddWithValue("@Bill_Date", (txtmaskbilldate.Text)); 

       } 



       if (txtcodecreator.Text == "") 
       { 
        cmd.Parameters.AddWithValue("@Code_Create", codecreate); 
       } 
       else 
       { 
        cmd.Parameters.AddWithValue("@Code_Create", txtcodecreator.Text.ToString()); 
       } 



       int n = cmd.ExecuteNonQuery(); 
       cnn.Close(); 
       if (n > 0) 
       { 
        MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 

cevap

0

Sen check benim insert kodunu vererek olduğunu. Ama eğer değilse, metin kutusunun içeriğini yine de parametre değeri olarak kullanın. - Bu kesinlikle doğru değil.

value DBNull olarak başlatılmışsa, parametreyi her durumda ekleyebilir ve kodunuzun başka bir bölümüne gerek duymazsınız. o, geçersiz @Bill_Date parametre için var olsa bile bir metin kutusuna metin ekleme ile

DateTime chequeDate; 
var value = (object)DBNull.Value; 
if (DateTime.TryParseExact(txtmaskchequedate.Text,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out chequeDate)) 
{ 
    value = chequeDate; 
} 
cmd.Parameters.AddWithValue("@Cheque_Date", value); 

aynı sorun: kodunuzu değiştirin . Numune de göre bu parametrenin kodunu yeniden yazınız.

+0

hala aynı istisna Ben bu DateTime chequeDate gibi kodu değiştirdim var; var value = (object) DBNull.Value; (DateTime.TryParseExact (txtmaskchequedate.Text, "dd/AA/yyyy", null, System.Globalization.DateTimeStyles.None, out chequeDate)) { value = chequeDate; } cmd.Parameters.AddWithValue ("@ Cheque_Date", value); Bunun için hata ayıklayıcısını da koydum ama hata ayıklayıcı cmd.Parameters.AddWithValue ("@ Cheque_Date", value); Bu satır sadece maskelenmiş metin kutusu boşsa – Atul

+0

hala aynı hata – Atul

+0

Cevabında kodu test ettim. Her iki durumda da çalışır. Kodunuzu debugger ile adım atmalısınız. ... Parameters.AddWithValue ... ile satır her zaman yürütülmelidir. – PhilS

İlgili konular