Saving Binary Image to Database using C# and VB.Net in ASP.Net

Last Reply 29 days ago By Andrea

Posted 29 days ago

I have one doubt

actually i am submitting data (id,name & image) in database (sql server 2012) but after submission image path is in hexa-decimal for rather than path of the image.

public partial class insertcategory : System.Web.UI.Page
{
    myclass c1= new myclass();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnsubmit_cgr_Click(object sender, EventArgs e)
    {
        string str = null;
        if(FileUpload1.HasFile)
        {
            str="~/admin_cgr_image/" + FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath(str));
        }
 
        string qu = "insert into Admin_New_Cgrr(cat_id,cat_name,cat_image)values(" + txtbox_catid.Text + ",'" + txtbox_catname.Text + "','" + str + "')";
        bool isvalidate = c1.executesql(qu);
        if (isvalidate == true)
        {
            lblmsg_cgr.Visible = true;
            lblmsg_cgr.Text = "Record Submitted";
        }
        else
        {
            lblmsg_cgr.Visible = true;
            lblmsg_cgr.Text = "Error occurred, please try again..!!!";
        }
    }
}

 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div class="admin_new_cgr">
    <fieldset style="height:450px; text-align:center;">
 
        <legend>
            <h1>Add New Category</h1>
        </legend>
 
        <table class="auto-style1">
            <tr>
                <td class="auto-style4">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style4">Category_ID :</td>
                <td style="text-align: left">
                    <asp:TextBox ID="txtbox_catid" runat="server" required="required" AutoPostBack="True"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style4">Category_Name :</td>
                <td style="text-align: left">
                    <asp:TextBox ID="txtbox_catname" runat="server" required="required"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style4">Image :</td>
                <td class="auto-style3">
                    <asp:FileUpload ID="FileUpload1" runat="server" style="text-align: center" />
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td style="text-align: left">
                    <asp:Label ID="lblmsg_cgr" runat="server" Text="*"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td style="text-align: left">
                    <asp:Button ID="btnsubmit_cgr" runat="server" Height="46px" Text="Submit" Width="143px" OnClick="btnsubmit_cgr_Click" />
                </td>
            </tr>
        </table> 
    </fieldset>
    </div>
</asp:Content>

 

 

You are viewing reply posted by: Andrea 29 days ago.
Posted 29 days ago
Hi @Amit1992,
Please try the following

Saving Image (Binary Data) to SQL Database and Display in GridView using C# in ASP.Net

It might help you.

Cheers Andrea.