How to import the excel sheet with images and save the data in database using asp .net

Last Reply one day ago By dharmendr

Posted 9 days ago

Hello Sir,

How to import the excel sheet with images and save the data in database using asp .net?

Please help me

Thanks

You are viewing reply posted by: pandeyism 8 days ago.
Posted 8 days ago

Hi democloud,

You cannot override the same ItemTemplate two times in TemplateField.

Refer below sample code.

HTML

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" EnableModelValidation="True">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <asp:Label ID="lblId" runat="server" Text='<% #Eval("ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Customer">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<% #Eval("Customer") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="lblPrice" runat="server" Text='<% #Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product Description">
            <   >
                <asp:Label ID="lblDescription" runat="server" Text='<% #Eval("Product") %>'></asp:Label>
                <asp:Image ID="image1" runat="server" ImageUrl="~/ClickHere.png" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button Text="Submit" runat="server" OnClick="Button2_Click" />

Namespaces

C#

using System.Data;

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("ID", typeof(int)), new DataColumn("Customer", typeof(string)), new DataColumn("Country", typeof(string)), new DataColumn("Product", typeof(string)) });
    dt.Rows.Add(1, "Mudassar", "India", "Bike");
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Button2_Click(object sender, EventArgs e)
{
    foreach (GridViewRow g1 in GridView1.Rows)
    {
        string ID = (g1.FindControl("lblId") as Label).Text;
        string Customer = (g1.FindControl("lblName") as Label).Text;
        string Country = (g1.FindControl("lblPrice") as Label).Text;
        string Product = (g1.FindControl("lblDescription") as Label).Text;
        Image Logo = g1.FindControl("image1") as Image;
        string query = "insert into Table values(" + ID + ",'" + Customer + "'," + Country + ",'" + Product + "','" + Logo.ImageUrl + "')";
    }
}