使用存储过程将表单数据和图像插入数据库 [英] inserting form data and image into database using store procedure

查看:81
本文介绍了使用存储过程将表单数据和图像插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生m从包含文本框的下拉菜单中选择文件上传m,使用这些代码使用storeprocedure将值和图像插入数据库,但值和图像未插入数据库m中没有出什么问题..pls预先感谢您.

m从数据库中填充城市和地点下拉列表,并将那里的ID插入数据库,但无法插入...

输入类型的格式不正确."

现在我得到了这个错误

没有从对象类型System.Web.HttpPostedFile到已知托管提供程序本机类型的映射."

存放程序:

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:

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:

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:

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:

<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>

推荐答案

Try replacing:
Try replacing:
cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.PostedFile);

with

cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.FileBytes);


Thanks for the help but i solved it..
just modified this line
cmdins.Parameters.AddWithValue("@Property_Image", FileUpload1.PostedFile);

cmdins.Parameters.AddWithValue("@Property_Image", imagebytes);
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);


这篇关于使用存储过程将表单数据和图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆