Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir m having a from contaning textboxes dropdowns fileupload m using these codes to insert values and image to database using storeprocedure but the value and image is not inserting in database m not getting what is wrong..pls help thanks in advance.

m filling city and locality dropdownlist from database and inserting there id to database but not able to insert...

"input type not in correct format error."

now i got this error

"No mapping exists from object type System.Web.HttpPostedFile to a known managed provider native type."

Store procedure:
SQL
ALTER Procedure dbo.Post_Property_Sale
(
@Property_Type varchar(50),@City_Id int,@locality_Id int,@Address varchar(200),
@Pincode numeric,@Price varchar(20),@Bedrooms int,@Build_up_area varchar(20),
@Key_Features varchar(200),@Property_Image image,@Name varchar(50),
@Email varchar(50),@Mobile_No numeric 
)
as
begin
insert into tbl_post_property_sale
(
    Property_Type,City_Id,Locality_Id,[Address],
    Pincode,Price,Bedrooms,Build_up_area ,
    Key_Features,Property_Image,Name ,
    Email,Mobile_No
)
values
(
@Property_Type,@City_Id,@locality_Id,@Address,
@Pincode,@Price,@Bedrooms,@Build_up_area,
@Key_Features,@Property_Image,@Name,
@Email,@Mobile_No 
)
end


Database Design:
SQL
create Table tbl_post_property_sale
(
	P_ID int identity(1,1)primary key,
	Property_Type varchar(50)not null,
	City_Id int references tbl_City(City_Id) not null,
	Locality_Id int references tbl_Locality(Locality_Id)not null,
	[Address] varchar(200)null,
	Pincode numeric null,
	Price varchar(20) not null,
	Bedrooms int null,
	Build_up_area varchar(20) not null,
	Key_Features varchar(200)null,
	Property_Image image not null,
	Name varchar(50)not null,
	Email varchar(50)not null,
	Mobile_No numeric not null  
)


Code Behind:
C#
int filelength = FileUpload1.PostedFile.ContentLength;
        byte[] imagebytes = new byte[filelength];
        FileUpload1.PostedFile.InputStream.Read(imagebytes,0,filelength);
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmdins = new SqlCommand("dbo.Post_Property_Sale", con);
        cmdins.CommandType = CommandType.StoredProcedure;
        cmdins.Parameters.AddWithValue("@Property_Type", ddlpropertytype.SelectedItem.Text);
        cmdins.Parameters.AddWithValue("@City_Id", Convert.ToInt32(ddlcity.SelectedItem.Value));
        cmdins.Parameters.AddWithValue("@Locality_Id", Convert.ToInt32(ddllocality.SelectedItem.Value));
        cmdins.Parameters.AddWithValue("@Address", txtaddress.Text);
        cmdins.Parameters.AddWithValue("@Pincode", txtpincode.Text);
        cmdins.Parameters.AddWithValue("@Price", txtprice.Text +' '+ ddlprice.SelectedItem.Text);
        cmdins.Parameters.AddWithValue("@Bedrooms", Convert.ToInt32(ddlbedrooms.SelectedItem.Text));
        cmdins.Parameters.AddWithValue("@Build_up_area", txtarea.Text + ' ' + ddlarea.SelectedItem.Text);
        cmdins.Parameters.AddWithValue("@Key_Features", txtfeatures.Text);
        cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.PostedFile);
        cmdins.Parameters.AddWithValue("@Name", txtname.Text);
        cmdins.Parameters.AddWithValue("@Email", txtemail.Text);
        cmdins.Parameters.AddWithValue("@Mobile_No", txtmobileno.Text);

        con.Open();

        cmdins.ExecuteNonQuery();
        lblmessage.Text = "Submitted successfully";
        con.Close();


Form Design:
ASP.NET
<table cellpadding="0" width="100%" cellspacing="20" border="0px" style="background-color: White">
            <tr>
                <td colspan="2" style="font-size: small; height: 35px;">
                    <span style="font-family: Arial">Fields marked with 
                    </span> 
                    <span style="color: #FF3300; font-family: Arial;">*</span><span 
                        style="font-family: Arial"> are 
                    mandatory:
                </span>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                   <asp:Label ID="lblmessage" runat="server" ForeColor="Green" Text=""></asp:Label> </td>
            </tr>
            <tr>
                <td>
                     
                </td>
                <td>
                    <span style="font-size: medium; font-family: Arial">Property:</span><br />
                    <asp:RadioButton ID="rbtnfs" Checked="true" Text="For Sale" GroupName="radio" runat="server"></asp:RadioButton>
                    
                    <asp:RadioButton ID="rbtnfr" Text="For Rent" GroupName="radio" runat="server"></asp:RadioButton>  
                    <span style="font-size: x-small; font-family: Arial; ">       
                    <span style="text-decoration: underline">Select Atleast One Option</span></span><br />
                    <asp:Label ID="lblerror" ForeColor="Red" runat="server" Text=""></asp:Label>
                   
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Property Type :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:DropDownList ID="ddlpropertytype" runat="server" CssClass="validate[required] radio"
                        Width="270px">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                        <asp:ListItem Value="Commercial">Commercial</asp:ListItem>
                        <asp:ListItem Value="Residential">Residential</asp:ListItem>
                        <asp:ListItem Value="Plot">Plot</asp:ListItem>
                        <asp:ListItem Value="Bunglow">Bunglow</asp:ListItem>
                        <asp:ListItem Value="Villa">Villa</asp:ListItem>
                    </asp:DropDownList>
                    <%--<asp:TextBox ID="txtfname" runat="server" CssClass="validate[required]" />--%>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">City :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:DropDownList ID="ddlcity" runat="server" Height="22px" CssClass="validate[required] radio"
                        Width="270px">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                       
                    </asp:DropDownList>
                    <%--<asp:TextBox ID="txtlname" runat="server" CssClass="validate[required]" />--%>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Locality :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:DropDownList ID="ddllocality" runat="server" Height="22px" CssClass="validate[required] radio"
                        Width="270px">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                        
                    </asp:DropDownList>
                    <%--<asp:TextBox ID="txtemail" runat="server" CssClass="validate[required,custom[email]" />--%>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <span style="font-size: small; font-family: Arial">Address :</span>
                </td>
                <td>
                    <asp:TextBox ID="txtaddress" runat="server" Height="84px" TextMode="MultiLine" Width="268px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Pincode :</span>
                </td>
                <td>
                    <asp:TextBox ID="txtpincode" runat="server" CssClass="validate[custom[onlyNumberSp]]"
                        Width="165px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Price :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtprice" runat="server" CssClass="validate[required,custom[number]]"
                        Width="162px"></asp:TextBox>
                        
                    <asp:DropDownList ID="ddlprice" runat="server" CssClass="validate[required] radio">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                        <asp:ListItem Value="cro">Crores</asp:ListItem>
                        <asp:ListItem Value="lak">Lakhs</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Bedrooms :
                </td>
                <td>
                    <asp:DropDownList ID="ddlbedrooms" runat="server"
                        Width="270px">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                        <asp:ListItem Value="1">0</asp:ListItem>
                        <asp:ListItem Value="2">1</asp:ListItem>
                        <asp:ListItem Value="3">2</asp:ListItem>
                        <asp:ListItem Value="4">3</asp:ListItem>
                        <asp:ListItem Value="5">4</asp:ListItem>
                        <asp:ListItem Value="6">5</asp:ListItem>
                        <asp:ListItem Value="7">6</asp:ListItem>
                        <asp:ListItem Value="8">7</asp:ListItem>
                        <asp:ListItem Value="9">8</asp:ListItem>
                        <asp:ListItem Value="10">9</asp:ListItem>
                        <asp:ListItem Value="11">10</asp:ListItem>
                    </asp:DropDownList>
                    <%--<asp:DropDownList ID="ddlState" runat="server" CssClass="validate[required] radio">
                                   
                                </asp:DropDownList>--%>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Built Up Area :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtarea" runat="server" CssClass="validate[required,custom[number]]"
                        Width="134px" ></asp:TextBox>
                     <asp:DropDownList ID="ddlarea" runat="server" CssClass="validate[required] radio">
                        <asp:ListItem Value="">--Select--</asp:ListItem>
                        <asp:ListItem Value="fe">sqft</asp:ListItem>
                        <asp:ListItem Value="me">sqm</asp:ListItem>
                        <asp:ListItem Value="ya">sqyds</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td valign="top" style="font-size: small; font-family: Arial">
                    Key Features :
                </td>
                <td>
                    <asp:TextBox ID="txtfeatures" runat="server" Height="83px" TextMode="MultiLine" Width="270px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <span style="font-size: small; font-family: Arial">UploadImage :</span>
                </td>
                <td valign="middle">
                    <asp:FileUpload ID="FileUpload1" runat="server" CssClass="validate[custom[fileupload]]">
                    </asp:FileUpload>
                     
                    <%--<asp:Button class="btn" ID="btnupload" runat="server" UseSubmitBehavior="false" 
                        Text="Upload" onclick="btnupload_Click" />--%>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" Text="*" ToolTip="Image Size Should Not Exceed 2 MB"
                        ErrorMessage="FileSize Exceeds 2MB.Please Try uploading smaller size files."
                        ControlToValidate="FileUpload1" OnServerValidate="checkfilesize" SetFocusOnError="true" />
                    <asp:ValidationSummary ID="ValidationSummary1" ForeColor="Red" runat="server" />
                    <br />
                    <span style="font-size: small; font-family: Arial">Upload only : </span><span
                        style="font-size: small; font-family: Arial">.GIF</span><span style="font-size: small;
                            font-family: Arial">, </span><span style="font-size: small; font-family: Arial">.PNG</span><span
                                style="font-size: small; font-family: Arial"> or </span><span style="font-size: small;
                                    font-family: Arial">.JPG</span><span style="font-size: small; font-family: Arial">.
                                        Maximum file size: </span><span style="font-size: small; font-family: Arial">2MB</span><span
                                            style="font-size: small; font-family: Arial">. </span>
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Your Name :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server" CssClass="validate[required,custom[onlyLetterSp]]"
                        Width="270px"></asp:TextBox>
                    <%--<asp:TextBox ID="txtZip" runat="server" CssClass="validate[required,custom[integer]] text-input" />--%>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Your Email :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server" CssClass="validate[required,custom[email]"
                        Width="270px"></asp:TextBox>
                    <%-- <input class="validate[required] checkbox" type="checkbox" id="agree" name="agree" />--%>
                </td>
            </tr>
            <tr>
                <td>
                    <span style="font-size: small; font-family: Arial">Mobile No :</span><span style="color: #FF0000">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtmobileno" runat="server" CssClass="validate[required,custom[mobile]]"
                        Width="270px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button class="btn" ID="btnSubmit" runat="server" Text="Submit" Width="98px" OnClick="btnSubmit_Click" />
                         
                    <asp:Button class="btn" ID="btnreset" runat="server" type="reset" UseSubmitBehavior="false" Text="Reset"
                        Width="98px" OnClick="btnreset_Click" />
                </td>
            </tr>
           
        </table>
Posted
Updated 28-Sep-12 19:25pm
v2
Comments
AmitGajjar 29-Sep-12 2:42am    
can you add try...catch block in your code and check what error occur ? also check if SP is called with your given values.

Try replacing:
C#
cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.PostedFile);
with
C#
cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.FileBytes);
 
Share this answer
 
Thanks for the help but i solved it..
just modified this line
cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.PostedFile);
with
cmdins.Parameters.AddWithValue("@Property_Image", imagebytes);
 
Share this answer
 

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