
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class offersettings : System.Web.UI.Page
{
SqlCommand cmd;
string getCon;
SqlConnection con;
SqlDataAdapter da;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if(Session ["a"]==null)
{
Response.Redirect("adminlogin.aspx");
}
else
{
getCon = ConfigurationManager.AppSettings["SqlCon"];
con = new SqlConnection(getCon);
if (IsPostBack != true)
{
FillGrid();
}
}
}
public void FillGrid()
{
con.Open();
cmd = new SqlCommand("select * from offersettings ", con);
cmd.CommandType = CommandType.Text;
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
grdContact.DataSource = dt;
grdContact.DataBind();
con.Close();
}
protected void grdContact_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label catcher = (Label)grdContact.Rows[e.RowIndex].FindControl("catcher");
Label lblId = (Label)grdContact.Rows[e.RowIndex].FindControl("lblId");
TextBox txtconfig=(TextBox)grdContact.Rows[e.RowIndex].FindControl("txtconfig");
TextBox txtamount=(TextBox)grdContact.Rows[e.RowIndex].FindControl("txtamount");
CheckBox chkActive = (CheckBox)grdContact.Rows[e.RowIndex].FindControl("chkActive");
FileUpload image = (FileUpload)grdContact.Rows[e.RowIndex].FindControl("FileUpload1");
string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*~";
Random randNum = new Random();
char[] chars = new char[5];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < 5; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
string s = new string(chars);
//HttpFileCollection uploadFilCol = Request.Files;
//for (int i = 0; i < uploadFilCol.Count; i++)
//{
string ss = image.PostedFile.FileName;
HttpPostedFile file = image.PostedFile;
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
string c = fileName.ToString();
string[] b = c.Split('.');
string x = b[0].ToString();
string d = x.ToString() + s.ToString() + fileExt.ToString();
if (fileName != string.Empty)
{
//try
// {
if (fileExt == ".jpg" || fileExt == ".gif")
{
file.SaveAs(Server.MapPath(".//images//offer//") + d);
//MessageBox.Show(" " + d + " Successfully Uploaded");
}
else
{
file.SaveAs(Server.MapPath(".//images//offer//") + d);
//MessageBox.Show(" " + d + " Successfully Uploaded");
}
con.Open();
cmd = new SqlCommand("update offersettings set image='" + d.ToString() + "',config='" + txtconfig.Text + "',amount='" + txtamount.Text + "' where id='" + lblId.Text + "'", con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
//contact.Update(txtName.Text, ddlSex.SelectedValue, cmbType.SelectedValue, flag,Convert.ToInt32(lblId.Text));
con.Close();
grdContact.EditIndex = -1;
FillGrid();
}
else
{
con.Open();
cmd = new SqlCommand("update offersettings set image='" + catcher.Text + "',config='" + txtconfig.Text + "',amount='" + txtamount.Text + "' where id='" + lblId.Text + "'", con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
//contact.Update(txtName.Text, ddlSex.SelectedValue, cmbType.SelectedValue, flag,Convert.ToInt32(lblId.Text));
con.Close();
grdContact.EditIndex = -1;
FillGrid();
}
// catch (Exception ex)
//{
//this.ShowMessage(" " + ex.Message, i);
//}
}
//ContactTableAdapter contact = new ContactTableAdapter();
//bool flag = false;
protected void grdContact_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
TextBox txtNewconfig = (TextBox)grdContact.FooterRow.FindControl("txtNewconfig");
TextBox txtnewamount = (TextBox)grdContact.FooterRow.FindControl("txtnewamount");
FileUpload image1 = (FileUpload)grdContact.FooterRow.FindControl("FileUpload2");
//if (chkNewActive.Checked) flag = true; else flag = false;
string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*~";
Random randNum = new Random();
char[] chars = new char[5];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < 5; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
string s = new string(chars);
//HttpFileCollection uploadFilCol = Request.Files;
//for (int i = 0; i < uploadFilCol.Count; i++)
//{
string ss = image1.PostedFile.FileName;
HttpPostedFile file = image1.PostedFile;
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
string c = fileName.ToString();
string[] b = c.Split('.');
string x = b[0].ToString();
string d = x.ToString() + s.ToString() + fileExt.ToString();
if (fileName != string.Empty)
{
try
{
if (fileExt == ".jpg" || fileExt == ".gif")
{
file.SaveAs(Server.MapPath(".//images//offer//") + d);
//MessageBox.Show(" " + d + " Successfully Uploaded");
}
else
{
file.SaveAs(Server.MapPath(".//images//offer//") + d);
//MessageBox.Show(" " + d + " Successfully Uploaded");
}
}
catch (Exception ex)
{
//this.ShowMessage(" " + ex.Message, i);
}
}
con.Open();
cmd = new SqlCommand("insert into offersettings values('" + d.ToString() + "','"+txtNewconfig.Text+"','"+txtnewamount.Text+"')", con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
//contact.Insert(txtNewName.Text, ddlNewSex.SelectedValue, cmbNewType.SelectedValue, flag);
FillGrid();
}
}
protected void grdContact_RowEditing(object sender, GridViewEditEventArgs e)
{
grdContact.EditIndex = e.NewEditIndex;
FillGrid();
}
protected void grdContact_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblId = (Label)grdContact.Rows[e.RowIndex].FindControl("lblId");
cmd = new SqlCommand("delete from offersettings where id = '" +lblId.Text+"'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillGrid();
}
}