Пример #1
0
    protected void simpanBer_Click(object sender, EventArgs e)
    {
        if (katberita.SelectedItem.Value != "")
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    if (FileUploadControl.PostedFile.ContentType == "image/jpeg" || FileUploadControl.PostedFile.ContentType == "image/png")
                    {
                        if (FileUploadControl.PostedFile.ContentLength < 102400)
                        {
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                            FileUploadControl.SaveAs(Server.MapPath("../photoberita/") + filename);

                            try
                            {
                                string strConn = WebConfigurationManager.ConnectionStrings["berita"].ConnectionString;
                                koneksi = new SqlConnection(strConn);
                                koneksi.Open();
                                string     sql  = @"select ID_Kategori from Kategori where NamaKategori = @NamaKategori";
                                SqlCommand cmd1 = new SqlCommand(sql, koneksi);
                                cmd1.Parameters.AddWithValue("@NamaKategori", katberita.SelectedItem.Text);
                                SqlDataReader dr = cmd1.ExecuteReader();
                                if (dr.Read())
                                {
                                    string id           = dr.GetString(0);
                                    string idberita1    = txtidberita.Text;
                                    string idkatberita1 = id;
                                    string tanggal1     = lbldatetime.Text;
                                    string judul1       = txtjudul.InnerText;
                                    string isiberita1   = textareas.InnerText;
                                    string sumber1      = txtsumber.InnerText;
                                    string idadmin1     = txtidadmin.Text;

                                    crud test   = new crud();
                                    int  result = test.simpanberita(idberita1, idkatberita1, tanggal1, judul1, isiberita1, sumber1, idadmin1, filename);
                                    Response.Write("<script>alert('Data Success Added');</script>");
                                    reset();
                                    Response.Redirect("AddNews.aspx");

                                    if (result != 0)
                                    {
                                        Response.Redirect("AddNews.aspx");
                                    }
                                }
                            }
                            catch (Exception cek)
                            {
                                StatusLabel.Text = cek.Message;
                            }
                            finally
                            {
                                koneksi.Close();
                            }
                        }
                        else
                        {
                            StatusLabel.Text = "Status: The file has to be less than 100 kb!";
                        }
                    }
                    else
                    {
                        StatusLabel.Text = "Status: Only JPEG AND PNG files are accepted!";
                    }
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
    }