【问题标题】:Data has not been inserted into database (using SqlDataSource)数据尚未插入数据库(使用 SqlDataSource)
【发布时间】:2016-01-08 04:05:15
【问题描述】:

我无法将表单中输入的值传递到 ASP.NET 的数据库中。我有两个 DropDownList 控件、两个 TextBox 控件和 1 个带有 FILE 输入类型的 html 控件。这是我的代码:

这是我的设计页面:

<asp:Content ID="ContentCitizenProfile" runat="server" ContentPlaceHolderID="cpdCitizenProfile">

<asp:ScriptManager ID="smDDL" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upDDL" runat="server">
    <ContentTemplate>
     <div class="form-inline" style="margin-bottom:25px;">
<asp:Label ID="lblCType" runat="server" Text="Complaint Type:"/>  
 <asp:DropDownList ID="ddlCType" runat="server" CssClass="form-control" DataSourceID="SqlDataSource1" DataTextField="Comp_Type" DataValueField="Type_ID" AutoPostBack="True" ondatabound="DDLCTypeDataBound" style="margin-left:79px" >
</asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ComplaintWebsiteConnectionString2 %>" SelectCommand="SELECT * FROM [Complaint_Type]"></asp:SqlDataSource>
        <asp:RequiredFieldValidator ID="RfvDDLCompType" runat="server" ErrorMessage="Please Select Complaint Type" ControlToValidate="ddlCType" ForeColor="Red">*</asp:RequiredFieldValidator>
     </div>


<div class="form-inline" style="margin-bottom:25px;">
    <asp:Label ID="lblSubType" runat="server" Text="Complaint Sub Type:"/>
<asp:DropDownList ID="ddlSubType" runat="server" CssClass="form-control" DataSourceID="SqlDataSource2" DataTextField="Comp_SubType" DataValueField="Type_ID" ondatabound="DDLSubTypeDataBound" style="margin-left:50px">
</asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ComplaintWebsiteConnectionString2 %>" SelectCommand="SELECT * FROM [Complaint_SubType] WHERE ([Type_ID] = @Type_ID2)">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlCType" Name="Type_ID2" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:RequiredFieldValidator ID="rfvDDlSubType" runat="server" ErrorMessage="Please Select Complaint Sub Type" ControlToValidate="ddlSubType" ForeColor="Red">*</asp:RequiredFieldValidator>
</div>
  </ContentTemplate>
</asp:UpdatePanel>       

<div id="demo" class="form-inline" style="margin-bottom:25px;">
    <asp:Label ID="lblLocation" runat="server" Text="Location:"/>
    <asp:TextBox ID="txtLoc" CssClass="form-control" runat="server" style="margin-left:120px"></asp:TextBox>&nbsp;
    <button ID="lblFind" runat="server" class="btn btn-info btn-xs">Find location on map</button>
    <br/><br />
    <iframe src="CompLocation.html" height="300" width="600"></iframe>
    </div>


<div class="form-inline" style="margin-bottom:25px;">
    <asp:Label ID="lblDesc" runat="server" Text="Post Description:"></asp:Label>
    <asp:TextBox ID="txtDesc" CssClass="form-control" runat="server" TextMode="MultiLine" style="margin-left:70px"></asp:TextBox>
</div>

<div class="form-inline" style="margin-bottom:25px;">
    <asp:Label ID="lblFileUpload" runat="server" Text="Upload Image:" style="margin-right:86px"/>
    <input id="UpldFile" type="file" class="filestyle btn-info" data-iconName="glyphicon glyphicon-inbox" style="margin-right:20px"/>
  <img id="imgPreview" src="#" alt="" style="width:150px;height:150px" class="img-thumbnail img-responsive">

<div class="form-inline" style="margin-bottom:50px;">
<button id="btnReportComp" class="btn btn-success" onserverclick="btnPostComp">Report Complaint</button> 
<button id="btnReset" class="btn btn-danger" onserverclick="btnReset" style="margin-left:20px">Reset</button>
</div>
<asp:SqlDataSource ID="SqlDataSourceReport" runat="server" ConnectionString="<%$ ConnectionStrings:ComplaintWebsiteConnectionString %>" InsertCommand="INSERT INTO Citizen_Complaints(Comp_Type, Comp_SubType, Location, Description, Image) VALUES (@ctype,@subtype,@loc,@desc,@img)" SelectCommand="SELECT * FROM [Citizen_Complaints]">
    <InsertParameters>
        <asp:ControlParameter ControlID="ddlCType" Name="ctype" PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="ddlSubType" Name="subtype" PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="txtLoc" Name="loc" PropertyName="Text" />
        <asp:ControlParameter ControlID="txtDesc" Name="desc" PropertyName="Text" />
        <asp:FormParameter FormField="UpldFile" Name="img" />
    </InsertParameters>
</asp:SqlDataSource>

.cs 页面:

protected void btnReportComp(object Sender, EventArgs e)
    {
        SqlDataSourceReport.Insert();
    }

【问题讨论】:

  • 没有错误。但是数据库列是空的
  • 你有没有把这个InsertCommandType="StoredProcedure"加到&lt;asp:SqlDataSource里面也试着在做插入操作的时候放try catch。
  • @rajeshpanchal 这里没有任何存储过程。她没有正确地提出她的问题。我编辑了问题,还没有人批准。
  • 我认为您应该添加try..catch 以获得异常。这将更有利于发现问题。

标签: c# asp.net sql-server database


【解决方案1】:

删除sqldatasource并添加到按钮next点击方法:

protected void btnSave_Click(object Sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(Utils.Connection);
    SqlCommand cmd = new SqlCommand("citizen_complaints_i", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@TYPE", SqlDbType.VarChar).Value = ddlCType.SelectedValue;
    cmd.Parameters.Add("@SUBTYPE", SqlDbType.VarChar).Value = ddlSubType.SelectedValue;
    cmd.Parameters.Add("@LOCATION", SqlDbType.VarChar).Value = txtLoc.Text;
    cmd.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = txtDesc.Text;
    cmd.Parameters.Add("@IMAGE", SqlDbType.VarChar).Value = Utils.file_upload(fuImage);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        //...
    }
    finally
    {
        con.Close();
    }
}

【讨论】:

    【解决方案2】:

    将您的 InsertCommand 更改为以下内容:

    INSERT INTO Citizen_Complaints(Comp_Type, Comp_SubType, Location, Description, Image) VALUES (@ctype,@subtype,@loc,@desc,@img)
    

    【讨论】:

    • 确实。但列仍然为空
    • @Ashwini 我认为这是因为你的形象。发布您的设计代码
    • @Ashwini 将您的 html 图像输入控件更改为 asp:FileUpload 控件,然后将 FileUpload1.FileName 发送到您的数据库。我认为您的数据库中有 varchar 字段来存储文件名,但是您的代码尝试将整个图像发送到您的数据库。但是你的代码不是很清楚......我想帮助使它更有效并且看起来更好
    • 我已经将 Css 和 js 应用于文件上传,所以我使用了输入类型。而且我保留了图像以允许数据库中的空值。所以我在没有输入图像的情况下进行了检查,但其他值也没有传递给 db
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多