How to insert NULL value when TextBox is empty in MySql database using C# .Net

hothorasma
 
on Feb 12, 2015 01:42 AM
8896 Views

how if the uploaded file is empty then the automatic sets the default 0 in mysql. with asp.net

my code

 

protected void btnsimpan_Click(object sender, EventArgs e)
    {

        string fileName1 = string.Empty;
        string fileName2 = string.Empty;
        string fileName3 = string.Empty;
        string nama_saran = string.Empty;
        string fileName5 = string.Empty;
        if (FileUpload1.HasFile)
            fileName1 = Path.GetFileName(FileUpload1.FileName);
        if (FileUpload2.HasFile)
            fileName2 = Path.GetFileName(FileUpload2.FileName);
        if (FileUpload3.HasFile)
            fileName3 = Path.GetFileName(FileUpload3.FileName);
        if (FileUpload4.HasFile)
            nama_saran = Path.GetFileName(FileUpload4.FileName);
        if (FileUpload5.HasFile)
            fileName5 = Path.GetFileName(FileUpload5.FileName);

        if (!String.IsNullOrEmpty(fileName1) && FileUpload1.PostedFile != null)
        {

            FileUpload1.SaveAs(Server.MapPath("Pembentukan/") + fileName1);

        }

      
        if (!String.IsNullOrEmpty(fileName2) && FileUpload2.PostedFile != null)
        {
           
            FileUpload2.SaveAs(Server.MapPath("Pengelolaan/") + fileName2);

        }
       

        if (!String.IsNullOrEmpty(fileName3) && FileUpload3.PostedFile != null)
        {

            FileUpload3.SaveAs(Server.MapPath("Retribusi/") + fileName3);

        }
        // File Upload 4
        if (!String.IsNullOrEmpty(nama_saran) && FileUpload4.PostedFile != null)
        {

            FileUpload4.SaveAs(Server.MapPath("Skpd/") + nama_saran);

        }

        
        if (!String.IsNullOrEmpty(fileName5) && FileUpload4.PostedFile != null)
        {

            FileUpload5.SaveAs(Server.MapPath("Strukture/") + fileName5);

        }

        
        //Pertama
        string Queryinsert = "INSERT INTO tbl_lembaga (propinsi_id,kabupaten_id,sektor_id,perda_pembentukan,perda_pengelolaan,perda_retribusi,nama_lembaga,fungsi_id,peraturan,keterangan,tanggal,userid,file_sarana,strukture)" +
                             "VALUES(@propinsi_id,@kabupaten_id,@sektor_id,@perda_pembentukan,@perda_pengelolaan,@perda_retribusi,@nama_lembaga,@fungsi_id,@peraturan,@keterangan,@tanggal,@userid,@file_sarana,@strukture)";

        MySqlConnection con = new MySqlConnection(strConnString);
        MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = Queryinsert;
                cmd.Parameters.AddWithValue("@propinsi_id", cmbprovinsi.Text);
                cmd.Parameters.AddWithValue("@kabupaten_id", cmbkabupaten.Text);
                cmd.Parameters.AddWithValue("@sektor_id", cmbsektor.Text);
                cmd.Parameters.AddWithValue("@nama_lembaga", txtnmlembaga.Text);
                cmd.Parameters.AddWithValue("@fungsi_id", cmbfungsi.Text);
                cmd.Parameters.AddWithValue("@peraturan", txtpembentukan.Text);
                cmd.Parameters.AddWithValue("@keterangan", txtketerangan.Text);
                cmd.Parameters.AddWithValue("@tanggal", DateTime.Now);
                cmd.Parameters.AddWithValue("@userid", txtuser.Text);
                cmd.Parameters.AddWithValue("@perda_pembentukan", "Pembentukan/" + fileName1);
                cmd.Parameters.AddWithValue("@perda_pengelolaan", "Pengelolaan/" + fileName2);
                cmd.Parameters.AddWithValue("@perda_retribusi", "Retribusi/" + fileName3);
               
                cmd.Parameters.AddWithValue("@file_sarana", "Skpd/" + nama_saran);
                cmd.Parameters.AddWithValue("@strukture", "strukture/" + fileName5);
                             
                            try
                            {
                                con.Open();
                                cmd.ExecuteNonQuery();
                            }

                            finally
                            {
                            lblerror.Text="Data disimpan..!!";
                            }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Feb 15, 2015 10:05 AM

1. Make sure your Column allows NULL in database.

2. Then for the column you need to insert NULL do coding as follows

if(!string.IsNullOrEmpty(cmbprovinsi.Text))
{
   cmd.Parameters.AddWithValue("@propinsi_id", cmbprovinsi.Text);
}
else
{
   cmd.Parameters.AddWithValue("@propinsi_id", DBNULL.Value);
}