Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASPX Code :

Image is not getting uploaded in Database. Please Help.

XML
<asp:Content ID="Content1" ContentPlaceHolderID="body" runat="server">
     <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
                    <h1 class="page-header">Testimonials</h1>
         <div class="table-responsive">
             <div class="mysidebar-ele">
                 <dx:ASPxLabel ID="ASPxLabel1" runat="server" Text="Name"></dx:ASPxLabel>
             </div>
             <div class="mysidebar-ele">
                 <dx:ASPxTextBox ID="txt_Name" runat="server" Width="170px"></dx:ASPxTextBox>
             </div>
                 <div class="mysidebar-ele">
                 <dx:ASPxLabel ID="ASPxLabel2" runat="server" Text="City"></dx:ASPxLabel>
             </div>
             <div class="mysidebar-ele">
                  <asp:DropDownList ID="ddl_City" Width="20%" DataSourceID="DS_City" CssClass="form-control" DataTextField="CityName"
                                                    DataValueField="CityId" AutoPostBack="true" runat="server" ToolTip="City" Font-Size="10" AppendDataBoundItems="true"/>
                   <asp:SqlDataSource ID="DS_City" runat="server" ConnectionString="<%$ ConnectionStrings:dbconn %>" SelectCommand="sp_city" SelectCommandType="StoredProcedure" />
             </div>
                 <div class="mysidebar-ele">
                 <dx:ASPxLabel ID="ASPxLabel3" runat="server" Text="Deal_Details"></dx:ASPxLabel>
             </div>
             <div class="mysidebar-ele">
                 <dx:ASPxTextBox ID="txt_DealDetails" runat="server" Width="170px"></dx:ASPxTextBox>
             </div>
                 <div class="mysidebar-ele">
                 <dx:ASPxLabel ID="ASPxLabel4" runat="server" Text="Comments"></dx:ASPxLabel>
             </div>
             <div class="mysidebar-ele">
                <textarea style="width:350px; height:100px;" placeholder="Enter Comment" runat="server" id="txt_comments"></textarea>
             </div>
             <%--  <div class="mysidebar-ele">
                 <dx:ASPxLabel ID="ASPxLabel5" runat="server" Text="Upload Photo"></dx:ASPxLabel>
             </div>--%>
             <div class="mysidebar-ele">

                 <dx:ASPxUploadControl ID="ASPxUploadControl1" runat="server" UploadMode="Auto" Width="280px"
    ShowProgressPanel="true" ShowUploadButton="true"  >
    <ValidationSettings MaxFileSize="4194304" AllowedFileExtensions=".jpg,.jpeg,.gif,.png" /></dx:ASPxUploadControl>
             </div>
             <div style="text-align:center">
                 <div class="mysidebar-ele">
                     <dx:ASPxButton ID="ASPxButton1" runat="server" Text="Add" OnClick="ASPxButton1_Click"></dx:ASPxButton>
             </div>
                   <p>
                                                <asp:Label ID="lbl_result" runat="server" Text=""></asp:Label>
                                            </p>
                 </div>
             </div>
         </div>

</asp:Content>




C# (BackEnd Code) :

protected void ASPxButton1_Click(object sender, EventArgs e)
{
if (!IsPostBack == false)
{
if (string.IsNullOrEmpty(txt_Name.Text))
{

lbl_result.Text = "Name Required !!";
return;
}

if (string.IsNullOrEmpty(txt_DealDetails.Text))
{
lbl_result.Text = "Deal Details Required !!";
return;
}

if (string.IsNullOrEmpty(txt_comments.Value))
{
lbl_result.Text = "Comment Required !!";
return;
}
//protected void UploadTest()
//{
//string filePath = Server.MapPath("~/Brochures/" + UploadTest.FileName);
//UploadTest.SaveAs(filePath);
//}
//if (string.IsNullOrEmpty(ASPxUploadControl1.))
//{
// lbl_result.Text = "Comment Required !!";
// return;
//}

string cons = WebConfigurationManager.ConnectionStrings["dbconn"].ToString();

SqlConnection conn = new SqlConnection(cons);
conn.Open();
SqlCommand cmd = new SqlCommand("sp_add_testimonials", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@state", "0");
cmd.Parameters.AddWithValue("@Name", txt_Name.Value);
cmd.Parameters.AddWithValue("@City", ddl_City.ToString());
cmd.Parameters.AddWithValue("@Deal_Details", txt_DealDetails.Value);
cmd.Parameters.AddWithValue("@Comments", txt_comments.Value);
cmd.Parameters.AddWithValue("@photo", ASPxUploadControl1.SaveAs("../../images/.jpg"));
cmd.ExecuteNonQuery();
conn.Close();
}
}
Posted
Updated 24-May-15 19:37pm
v2

please show the error you are getting. what is the data type of @photo in your stored procedure?

you have to convert that image into Byte Array before saving that complete image into database. there are so many examples. just search on Google like this one C# Save and Load Image from Database[^]
 
Share this answer
 
Comments
kunal024 25-May-15 2:45am    
Datatype for @photo is nvarchar(50). I am very new to this so sorry for such silly question
kashif Atiq 25-May-15 3:07am    
there is always a first time :) so don't worry make your datatype as varbinary(MAX) and then read this example

http://www.aspsnippets.com/Red/Articles/How-to-save-insert-Image-in-Database-in-ASPNet-using-C-and-VBNet.aspx

this example is using simple file upload control you can search for equivalent properties for your ASPxUploadControl1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900