2015-05-11 68 views
0

我想只插入選定的值,但當我保存它將所有值插入數據庫。 enter image description hereenter image description here如何避免將不需要的值插入數據庫?

在上面的數據庫subject列中,我只想要有能力的主題。但在我的數據庫中所有主題都插入了。

我使用boundfield作爲GridView中的主題欄。我的代碼到目前爲止`

<asp:GridView ID="Gvassignsubject" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gvassignsubject_RowDataBound"> 
     <Columns> 
      <asp:BoundField DataField="Subject" HeaderText="subject" SortExpression="subject" /> 
      <asp:TemplateField HeaderText ="Faculty"> 
       <ItemTemplate> 
        <asp:Label ID ="lblfaculty" runat="server" Text='<%%# Eval("facultyname") %>>' Visible="false"/> 
        <asp:DropDownList ID="ddlfaculty" runat="server"></asp:DropDownList> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Set Hour"> 
       <ItemTemplate> 
        <asp:TextBox ID="txthour" runat="server"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
      </Columns> 
    </asp:GridView>` 


protected void btnsubmit_Click(object sender, EventArgs e) 
    { 
     using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
     { 
      foreach (GridViewRow r in Gvassignsubject.Rows) 
      { 
       for (int i = 0; i < Gvassignsubject.Rows.Count; i++) 
       { 
        string batch = ddlbatches.SelectedValue; 
        string subject = r.Cells[0].Text; 
        DropDownList ddlfaculty = (DropDownList)Gvassignsubject.Rows[i].FindControl("ddlfaculty"); 
        TextBox txthour = (TextBox)Gvassignsubject.Rows[i].FindControl("txthour"); 
        string facultyname = ddlfaculty.SelectedValue; 
        string sethour = txthour.Text; 
        con2.Open(); 
        SqlCommand cmd = new SqlCommand("insert into assign (batch,facultyname,sethour,subject)values(@batch,@facultyname,@sethour,@subject)", con2); 
        cmd.Parameters.AddWithValue("@batch", batch); 
        cmd.Parameters.AddWithValue("@subject", subject); 
        cmd.Parameters.AddWithValue("@facultyname", facultyname); 
        cmd.Parameters.AddWithValue("@sethour", sethour); 
        cmd.Connection = con2; 
        cmd.ExecuteNonQuery(); 
        con2.Close(); 
       } 
      } 
     } 
    } 
+0

能否請您把支票ddlfaculty.SelectedValue一樣,如果(!(ddlfaculty.SelectedValue.ToString()==「請選擇」) –

回答

0

它添加所有的行,不管因爲沒有標準;

for (int i = 0; i < Gvassignsubject.Rows.Count; i++) 
    { 

     DropDownList ddlfaculty = (DropDownList)Gvassignsubject.Rows[i].FindControl("ddlfaculty"); 
     string facultyname = ddlfaculty.SelectedValue; 

     if (!string.Equals(ddlfaculty.SelectedValue, "Please Select")) 
     { 
      string batch = ddlbatches.SelectedValue; 
      string subject = r.Cells[0].Text; 
      TextBox txthour = (TextBox)Gvassignsubject.Rows[i].FindControl("txthour"); 
      string sethour = txthour.Text; 
      con2.Open(); 
      SqlCommand cmd = new SqlCommand("insert into assign (batch,facultyname,sethour,subject)values(@batch,@facultyname,@sethour,@subject)", con2); 
      cmd.Parameters.AddWithValue("@batch", batch); 
      cmd.Parameters.AddWithValue("@subject", subject); 
      cmd.Parameters.AddWithValue("@facultyname", facultyname); 
      cmd.Parameters.AddWithValue("@sethour", sethour); 
      cmd.Connection = con2; 
      cmd.ExecuteNonQuery(); 
      con2.Close(); 
     } 
    } 
+0

我試過這個,但輸出相同。它顯示數據庫中的所有主題。是否有任何foreach的替代? –

+0

它不應該是這種情況,因爲標準將阻止添加具有默認下拉列表值的行,你在調試器中檢查了你的代碼,看它是否正在做你所需要的嗎? – horHAY

0

我相信你已經設置默認的選擇列表項的值null並在for循環檢查它:

for (int i = 0; i < Gvassignsubject.Rows.Count; i++) { if(!string.IsNullOrEmpty(ddlfaculty.SelectedValue)) { ... } }

+0

不工作,結果相同。 –

+0

你能說出'ddlfaculty.SelectedValue'值是什麼嗎?爲你不想插入數據庫的行選擇? – Kamo

0

你可以把將檢查值的條件存在或不在主題領域。爲此,您可以象下面這樣:

if(facultyname !="" && facultyname !="Please Select") { SqlCommand cmd = new SqlCommand("insert into assign (batch,facultyname,sethour,subject)values(@batch,@facultyname,@sethour,@subject)", con2); cmd.Parameters.AddWithValue("@batch", batch); cmd.Parameters.AddWithValue("@subject", subject); cmd.Parameters.AddWithValue("@facultyname", facultyname); cmd.Parameters.AddWithValue("@sethour", sethour); cmd.Connection = con2; cmd.ExecuteNonQuery(); }

這是考慮到ddlfaculty下拉 具有價值跟它的文字一樣的條件。在分配給facultyname和做插入之前

+0

這不起作用,結果相同。是否有任何foreach的替代? –

+0

你能否建議facultyname的價值是什麼,當字符串facultyname = ddlfaculty.SelectedValue;' 當它在數據庫中沒有價值? – Nivs

+0

facultyname從下拉列表中選擇(ddlfaculty) –

0

試試這個

if(ddlfaculty.selectedindex !=-1) 
{ 
do the insert function 
}