2012-11-19 66 views
0

我是C#中ASP.NET的新開發人員。我開發了一個基於網絡的內聯網建議應用程序,作爲我部門員工提交的建議的一個工具庫。如何將選中的值以這種形式插入到數據庫中?

我有以下的數據庫設計:

Log Table: ID, Title, Description, DateSubmitted, Username, TypeID, StatusID 
SuggestionsType Table: ID, Type 
SuggestionsStatus Table: ID, Status 
Employee Table: Username, Name, Title 

我可以能夠與它的標題,datesubmitted,描述和用戶名通過下面的代碼插入建議:

ASP.NET代碼

<div ID="contactform"> 
       <ol> 
        <li> 
         <label for="subject"> 
         Type</label> 
         <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource1" Width="155px" 
            Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" 
            DataTextField="Type" DataValueField="ID" AutoPostBack="true" OnDataBound="DropDownList_DataBound"> 
         </asp:DropDownList> 
         <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT * FROM [SafetySuggestionsType]"></asp:SqlDataSource> 
         <asp:TextBox ID="TextBox1" runat="server" CssClass="text"></asp:TextBox> 
         <br /> 
         <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
          ControlToValidate="dropdownlist" 
          ErrorMessage="Please select a type for your suggestion or choose Others and put a type for it"></asp:RequiredFieldValidator> 
         <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
          ControlToValidate="TextBox1" 
          ErrorMessage="Please enter a type for your suggestion"></asp:RequiredFieldValidator> 
        </li> 
        <li> 
         <label for="subject"> 
         Subject</label> 
         <asp:TextBox ID="txtSubject" runat="server" CssClass="text"></asp:TextBox> 
         <br /> 
         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
          ControlToValidate="txtSubject" 
          ErrorMessage="Please enter a subject/title for your suggestion"></asp:RequiredFieldValidator> 
        </li> 

        <%--The following hidden field is for inserting the date--%> 
        <li> 
         <asp:TextBox ID="dateSubmitted" runat="server" CssClass="text" Visible="false"></asp:TextBox> 
         <br /> 
        </li> 

        <li> 
         <label for="message"> 
         Your Suggestion</label> 
         <asp:TextBox ID="txtSuggestion" runat="server" cols="50" CssClass="textarea" 
          rows="6" TextMode="MultiLine"></asp:TextBox> 
         <br /> 
         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
          ControlToValidate="txtSuggestion" ErrorMessage="Please enter your suggestion"></asp:RequiredFieldValidator> 
        </li> 
        <li class="buttons"> 
         <asp:ImageButton ID="imageField" runat="server" imageurl="images/Send.gif" 
          OnClick="btnSubmit_Click" /> 
         <%--<input type="image" name="imageField" id="imageField" src="images/Send.gif" />--%> 
        </li> 
       </ol> 
      </div> 

C#代碼:

string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=Test;Integrated Security=True"; 
     string insertCommand = "INSERT INTO Log (Title, DateSubmitted, Description, Username) values(@Title, @DateSubmitted, @Description, @Username)"; 
     string username = netID; 
     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand(insertCommand, conn)) 
      { 
       cmd.Parameters.Clear(); 
       cmd.Parameters.AddWithValue("@Title", txtSubject.Text); 
       cmd.Parameters.AddWithValue("@DateSubmitted", DateTime.Now.ToString()); 
       cmd.Parameters.AddWithValue("@Description", txtSuggestion.Text); 
       cmd.Parameters.AddWithValue("@Username", username); 
       cmd.ExecuteNonQuery(); 
      } 
     } 

我現在需要的是插入所選類型的值,那麼該怎麼做?

回答

2

添加seletedType一個參數在你的插入查詢:

string connString = "Data Source=dha00730-pmtq01\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True"; 
     string insertCommand = "INSERT INTO SafetySuggestionsLog (Title, DateSubmitted, Description, Username, TypeID) values(@Title, @DateSubmitted, @Description, @Username,@TypeID)"; 
     string username = netID; 
     int typeID = Convert.ToInt64(DropDownList.SelectedValue); 
     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand(insertCommand, conn)) 
      { 
       cmd.Parameters.Clear(); 
       cmd.Parameters.AddWithValue("@Title", txtSubject.Text); 
       cmd.Parameters.AddWithValue("@DateSubmitted", DateTime.Now.ToString()); 
       cmd.Parameters.AddWithValue("@Description", txtSuggestion.Text); 
       cmd.Parameters.AddWithValue("@Username", username); 
       cmd.Parameters.AddWithValue("@TypeID",typeID);  
       cmd.ExecuteNonQuery(); 
      } 
     } 
+0

感謝您的幫助,但**如何插入典型eID?**和類型將顯示爲一個下拉列表,如上面的ASP.NET代碼所示。 –

+0

是否想用typeID填充下拉列表框? –

+0

我使用RecommendationsType表中的類型填充它。 TypeID是RecommendationsType表中ID的外鍵。 –

0

的解決方案是相同的,以什麼@Rohit Vyas說在他的方案中提到,但我只是修改下面的代碼行:

cmd.Parameters.AddWithValue("@TypeID",typeID); 

下列要求:

cmd.Parameters.AddWithValue("@TypeID",DropDownList.SelectedIndex);  
相關問題