in .ASPX page
<asp:GridView ID="grd1" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" ID="img1" ImageUrl='<%# Eval("product_image","~/Images/{0}") %>' Height="30px" Width="30px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="prod_code" HeaderText="Code" />
<asp:BoundField DataField="prod_name" HeaderText="Name" />
<asp:BoundField DataField="prod_price" HeaderText="Price" />
<asp:BoundField DataField="prod_add_date" HeaderText="Date" />
</Columns>
</asp:GridView>
In .aspx.CS
private SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn1"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(@"select * from products", cn);
da.Fill(dt);
if (dt != null)
{
grd1.DataSource = dt;
grd1.DataBind();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
FileInfo image_file = new FileInfo(fu1.FileName);
string des_path = HttpContext.Current.Server.MapPath("") + "\\Images";
if (!Directory.Exists(des_path))
Directory.CreateDirectory(des_path);
string file_extension = image_file.Extension;
des_path += "\\" + txtCode.Text.Trim() + file_extension;
fu1.SaveAs(des_path);
SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[products]
([prod_code]
,[prod_name]
,[prod_price]
,[product_image]
,[prod_add_date])
VALUES
('" + txtCode.Text.Trim() + "','" + txtName.Text.Trim() + "','" + txtPrice.Text.Trim() + "','" + txtCode.Text + file_extension + "',GETDATE())", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}