protected void btnSave_Click(object sender, EventArgs e) { try { string institutename = txtinstitutename.Text; string category = Txtcategory.Text; string educationgroup = txteducationgroup.Text; string description = txtinstdescp.Text; string address = txtaddress.Text; string state = ddlstate.SelectedItem.ToString(); string city = ddlcity.SelectedItem.ToString(); string area = txtarea.Text; string town = txttown.Text; string country = txtcountry.Text; long zipcode = long.Parse(txtzipcode.Text.ToString()); string affiliatedto = ddlaffiliatedto.SelectedItem.ToString(); string board = ddlboard.SelectedItem.ToString(); string classfrom = ddlclassfrom.SelectedItem.ToString(); string medium = ddlmedium.SelectedItem.ToString(); string classto = ddlclassto.SelectedItem.ToString(); string schooltype = ddlschooltype.SelectedItem.ToString(); string classification = ddlclassification.SelectedItem.ToString(); string admissionproc = txtadmprocedure.Text; string docrequired = txtdocsreq.Text; int agecriteria = int.Parse(txtagecriteria.Text.ToString()); DateTime admstartdate = DateTime.Parse(txtadmstartdate.Text.ToString()); //string admstdate = admstartdate.ToString("YYYY-MM-DD"); DateTime admenddate = DateTime.Parse(txtadmenddate.Text.ToString()); //string admedate = admenddate.ToString("YYYY-MM-DD"); string contactname = txtyourname.Text; string email = txtemail.Text; string phone = txtphone.Text; string website = txtwebsite.Text; string designation = txtdesignation.Text; string facebook = txtfacebook.Text; string twitter = txttwitter.Text; string linkedin = txtlinkedin.Text; string skype = txtskype.Text; string gmaps = txtgmapslink.Text; con.Open(); SqlCommand cmd0 = new SqlCommand(@"INSERT INTO Schoolmaster(Schoolname,Category,Educationgroup,Ins_description,Address,State,City,Area,Town,Country,Zipcode,Googlemaps,Affiliatedto,Board,Medium,Class_from,Class_to,School_type,Classification,Adm_procedure,Docs_required,Age_criteria,Admstartdate,Admenddate,Contact_name,Contact_email,Contact_Phone,Designation,Website,Facebook,Twitter,Linkedin,Skype,Listingpriority,Package) values('" + institutename + "','" + category + "','" + educationgroup + "','" + description + "','" + address + "','" + state + "','" + city + "','" + area + "','" + town + "','" + country + "','" + zipcode + "','" + gmaps + "','" + affiliatedto + "','" + board + "','" + medium + "','" + classfrom + "','" + classto + "','" + schooltype + "','" + classification + "','" + admissionproc + "','" + docrequired + "','" + agecriteria + "','" + admstartdate + "','" + admenddate + "','" + contactname + "','" + email + "','" + phone + "','" + designation + "','" + website + "','" + facebook + "','" + twitter + "','" + linkedin + "','" + skype + "','low','Free');", con); cmd0.ExecuteNonQuery(); SqlCommand cmd1 = new SqlCommand("Select MAX(SchoolId) from Schoolmaster;", con); cmd1.ExecuteNonQuery(); SqlDataReader dr0 = cmd1.ExecuteReader(); while (dr0.Read()) // This is how record is read. Loop through the each record { var schoolid = long.Parse(dr0[0].ToString()); lblclassid.Text = schoolid.ToString(); } dr0.Close(); int Class_id = int.Parse(lblclassid.Text.ToString()); /////Loading CoachingFacilities for (int i = 0; i < cblfacilities.Items.Count; i++) { if (cblfacilities.Items[i].Selected == true) { SqlCommand cmd2 = new SqlCommand("insert into Schoolfacilities values ('" + Class_id + "','" + cblfacilities.Items[i].ToString() + "')", con); cmd2.ExecuteNonQuery(); } } ///////////Condition for checking values in fileupload box and then loading the table//////////// if (FileUpload1.HasFile && FileUpload2.HasFile && FileUpload3.HasFile) { FileInfo fi1 = new FileInfo(FileUpload1.FileName); string name1 = fi1.Name; byte[] image1 = FileUpload1.FileBytes; FileInfo fi2 = new FileInfo(FileUpload2.FileName); string name2 = fi2.Name; byte[] image2 = FileUpload2.FileBytes; FileInfo fi3 = new FileInfo(FileUpload3.FileName); string name3 = fi3.Name; byte[] image3 = FileUpload3.FileBytes; SqlCommand com = new SqlCommand("Insert into Schoolimages (SchoolId,Schoollogo, School_Image1,School_Image2) values (@SchoolId,@Schoollogo, @School_Image1,@School_Image2)", con); com.Parameters.AddWithValue("@SchoolId", SqlDbType.VarChar).Value = lblclassid.Text; com.Parameters.AddWithValue("@Schoollogo", SqlDbType.VarBinary).Value = image1; com.Parameters.AddWithValue("@School_Image1", SqlDbType.VarBinary).Value = image2; com.Parameters.AddWithValue("@School_Image2", SqlDbType.VarBinary).Value = image3; com.ExecuteNonQuery(); } else if (FileUpload1.HasFile && !FileUpload2.HasFile && !FileUpload3.HasFile) { FileInfo fi1 = new FileInfo(FileUpload1.FileName); string name1 = fi1.Name; byte[] image1 = FileUpload1.FileBytes; //FileInfo fi2 = new FileInfo(FileUpload2.FileName); //string name2 = fi2.Name; //byte[] image2 = FileUpload2.FileBytes; //FileInfo fi3 = new FileInfo(FileUpload3.FileName); //string name3 = fi3.Name; //byte[] image3 = FileUpload3.FileBytes; SqlCommand com = new SqlCommand("Insert into Schoolimages (SchoolId,Schoollogo ) values (@SchoolId,@Schoollogo)", con); com.Parameters.AddWithValue("@SchoolId", SqlDbType.VarChar).Value = lblclassid.Text; com.Parameters.AddWithValue("@Schoollogo", SqlDbType.VarBinary).Value = image1; //com.Parameters.AddWithValue("@School_Image1", SqlDbType.VarBinary).Value = image2; //com.Parameters.AddWithValue("@School_Image2", SqlDbType.VarBinary).Value = image3; com.ExecuteNonQuery(); } else if (!FileUpload1.HasFile && FileUpload2.HasFile && !FileUpload3.HasFile) { FileInfo fi2 = new FileInfo(FileUpload2.FileName); string name2 = fi2.Name; byte[] image2 = FileUpload2.FileBytes; SqlCommand com = new SqlCommand("Insert into Schoolimages (SchoolId, School_Image1) values (@SchoolId, @School_Image1)", con); com.Parameters.AddWithValue("@SchoolId", SqlDbType.VarChar).Value = lblclassid.Text; com.Parameters.AddWithValue("@School_Image1", SqlDbType.VarBinary).Value = image2; com.ExecuteNonQuery(); } else if (!FileUpload1.HasFile && !FileUpload2.HasFile && FileUpload3.HasFile) { FileInfo fi3 = new FileInfo(FileUpload3.FileName); string name3 = fi3.Name; byte[] image3 = FileUpload3.FileBytes; SqlCommand com = new SqlCommand("Insert into Schoolimages (SchoolId,School_Image2) values (@SchoolId,@School_Image2)", con); com.Parameters.AddWithValue("@SchoolId", SqlDbType.VarChar).Value = lblclassid.Text; com.Parameters.AddWithValue("@School_Image2", SqlDbType.VarBinary).Value = image3; com.ExecuteNonQuery(); } else if (FileUpload1.HasFile && !FileUpload2.HasFile && !FileUpload3.HasFile) { FileInfo fi1 = new FileInfo(FileUpload1.FileName); string name1 = fi1.Name; byte[] image1 = FileUpload1.FileBytes; SqlCommand com = new SqlCommand("Insert into Schoolimages (SchoolId,Schoollogo) values (@SchoolId,@Schoollogo)", con); com.Parameters.AddWithValue("@SchoolId", SqlDbType.VarChar).Value = lblclassid.Text; com.Parameters.AddWithValue("@Schoollogo", SqlDbType.VarBinary).Value = image1; com.ExecuteNonQuery(); } con.Close(); string display = "School Details Added Successfully!"; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true); txtinstitutename.Text = ""; Txtcategory.Text = ""; txteducationgroup.Text = ""; txtinstdescp.Text = ""; txtaddress.Text = ""; ddlstate.SelectedIndex = 0; ddlcity.SelectedIndex = 0; txtarea.Text = ""; txttown.Text = ""; txtcountry.Text = ""; txtzipcode.Text = ""; ddlaffiliatedto.SelectedIndex = 0; ddlboard.SelectedIndex = 0; ddlclassfrom.SelectedIndex = 0; ddlclassto.SelectedIndex = 0; ddlschooltype.SelectedIndex = 0; ddlclassification.SelectedIndex = 0; txtadmprocedure.Text = ""; txtdocsreq.Text = ""; txtagecriteria.Text = ""; txtadmstartdate.Text = ""; txtadmenddate.Text = ""; txtyourname.Text = ""; txtemail.Text = ""; txtphone.Text = ""; txtwebsite.Text = ""; txtdesignation.Text = ""; txtfacebook.Text = ""; txttwitter.Text = ""; txtlinkedin.Text = ""; txtskype.Text = ""; FileUpload1.Dispose(); FileUpload2.Dispose(); FileUpload3.Dispose(); } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex.Message + "');", true); } }