Hi, Sumeet
I have created sample code which fullfill your requirement.
try this way.
HTML
<div>
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="CustomerName">
<ItemTemplate>
<asp:TextBox ID="txtCustomerName" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pics">
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Save" OnClick="OnUpdate" runat="server" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGridview();
}
}
private void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("RowNumber", typeof(int));
for (int i = 1; i < 11; i++)
{
dt.Rows.Add(i);
}
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void OnUpdate(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
string customerName = (row.FindControl("txtCustomerName") as TextBox).Text;
string country = (row.FindControl("txtCountry") as TextBox).Text;
FileUpload upload = (FileUpload)row.FindControl("FileUpload1");
if (!string.IsNullOrEmpty(customerName) && !string.IsNullOrEmpty(country) && !string.IsNullOrEmpty(upload.PostedFile.FileName))
{
System.IO.Stream fs = upload.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO [Customers] VALUES(@Name, @Country,@Data)";
cmd.Parameters.AddWithValue("@Name", customerName);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}