Storing Images in a Database and Displaying in GridView

Self-Innovator
Posted by Self-Innovator under ASP.NET category on | Points: 40 | Views : 2274
Form Design
ManageCountry.aspx
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
Enter the Country Name
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Image
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Add" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<asp:GridView ID="grdDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<%#Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Iamge1" runat="server" ImageUrl=<%#Bind("CountryImage") %> Width="100px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<a href="ManageCountry.aspx?id=<%#Eval("id") %>">Edit</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>

ManageCountry.aspx.cs
PageLoad
string str = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
ConnectToDB db = new ConnectToDB();
DataTable dt;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillCountrys();
if (Request.QueryString["id"] != null)
{
int id = int.Parse(Request.QueryString["id"].ToString());
Edit(id);
}
}
}

Button Click
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (btnSubmit.Text == "Add")
{
int Count;
string counryImage = string.Empty;
string UniqueName = (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString());
if(FileUpload1.PostedFile.ContentLength>0)
{
string flag=Path.GetExtension(FileUpload1.PostedFile.FileName);
if (flag == ".jpeg" || flag == ".JPEG" || flag == ".jpg" || flag == ".JPG" || flag == ".png" || flag == ".PNG" || flag == ".bmp" || flag == ".BMP" || flag == ".gif" || flag == ".GIF" || flag == ".wmf" || flag == ".WMF")
{
string flag1;
string flag2 = Server.MapPath("~\\uploads")+"\\";
string flag3 = FileUpload1.PostedFile.FileName;
string[] words = flag3.Split('.');
flag1 = flag2 + UniqueName + flag3;
FileUpload1.PostedFile.SaveAs(flag1);
string url = "~\\" + "uploads" + "\\" + UniqueName + flag3;
counryImage = url.Replace("\\", "/");
}
}
ds = db.AddCountrys(txtCountry.Text.Trim(),counryImage);
Count = Convert.ToInt32(ds.Tables[0].Rows[0]["DuplicateCountry"].ToString());
if (Count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language='javascript'>alert('Country already exists')</script>");
}
else
{
txtCountry.Text = "";
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language='javascript'>alert('Country added successfully')</script>");
FillCountrys();
}
}
else
{
int id = int.Parse(Request.QueryString["id"].ToString());
string counryImage = string.Empty;
string UniqueName = (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString());
if (FileUpload1.PostedFile.ContentLength > 0)
{
string flag = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (flag == ".jpeg" || flag == ".JPEG" || flag == ".jpg" || flag == ".JPG" || flag == ".png" || flag == ".PNG" || flag == ".bmp" || flag == ".BMP" || flag == ".gif" || flag == ".GIF" || flag == ".wmf" || flag == ".WMF")
{
string flag1;
string flag2 = Server.MapPath("~\\uploads") + "\\";
string flag3 = FileUpload1.PostedFile.FileName;
string[] words = flag3.Split('.');
flag1 = flag2 + UniqueName + flag3;
FileUpload1.PostedFile.SaveAs(flag1);
string url = "~\\" + "uploads" + "\\" + UniqueName + flag3;
counryImage = url.Replace("\\", "/");
}
}
dt = db.UpdateCountrys(id, txtCountry.Text.Trim(),counryImage);
txtCountry.Text = "";
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language='javascript'>alert('Country Updated successfully')</script>");
FillCountrys();
btnSubmit.Text = "Add";
}
}

Methods
protected void FillCountrys()
{
dt = db.GetCountryList();
grdDetails.DataSource = dt;
grdDetails.DataBind();
}
protected void Edit(int id)
{
btnSubmit.Text = "Update";
dt = db.GetCountryById(id);
if (dt.Rows.Count == 0)
{

}
else
{
txtCountry.Text = dt.Rows[0]["Name"].ToString();
}
}

AppCode.Cs
   public DataTable GetCountryList()
{
SqlCommand cmd = new SqlCommand("sp_GetCountrys", cnn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}
public DataTable GetCountryById(int id)
{
SqlCommand cmd = new SqlCommand("sp_GetCountryById", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cnn.Open();
cmd.Parameters.AddWithValue("@id", id);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}

Comments or Responses

Login to post response