2016-01-08 90 views
1

我無法將表單中輸入的值傳遞到ASP.NET中的數據庫中。我有兩個DropDownList控件,兩個TextBox控件和1個帶有FILE輸入類型的html控件。這是我的代碼:數據尚未被插入到數據庫中(使用SqlDataSource)

這是我設計的頁面:

<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(); 
    } 
+0

什麼錯誤? – Mairaj

+0

沒有錯誤。但是數據庫列是空的 – Ash

+0

您是否在' pedram

回答

1

刪除SqlDataSource和加入到旁邊的按鈕點擊方法:

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(); 
    } 
} 
1

你的InsertCommand更改爲以下:

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

有。但仍然列無效 – Ash

+1

@ Ashwini我認爲這是因爲你的形象。發佈您的設計代碼 – Khazratbek

+0

我上面編輯了我的帖子。 – Ash