<div>
        <br />
        <br />
        <table class="tableBorder" cellspacing="1" cellpadding="3" align="center" border="0">
          <tbody>
            <tr>
              <th class="tableHeaderText" height="25">&nbsp;</th>
            </tr>
          </tbody>
          <tbody>
            <tr>
              <td height="23" class="forumRow"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
                <tr>
                  <td width="36%" align="right"><span style="width: 94px; height: 23px">班组名称:</span></td>
                  <td width="64%"><span style="height: 23px; width: 281px;">
                    <asp:TextBox ID="bzname" runat="server" Width="144px"></asp:TextBox>
                  &nbsp;
                      </span></td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td height="23" class="forumRow"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
                <tr>
                  <td width="36%" align="right"><span style="width: 94px">所属车间:</span></td>
                  <td width="64%">
                      <asp:DropDownList ID="DropDownList1" runat="server" 
                          DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id" 
                          >
                      </asp:DropDownList>
                      <span style="width: 281px">
                  &nbsp;&nbsp;&nbsp;
                  </span>
                    </td>
                </tr>
              </table></td>
            </tr>
           
            <tr>
              <td height="23" align="center" class="forumRow"><span style="height: 24px">
                <asp:Button ID="SButton" runat="server"  Text="添加" 
                      Height="23px" onclick="SButton_Click" />                
                <font
                        face="宋体"> &nbsp;</font>
                <asp:Button ID="Button2" runat="server" Text="取消" onclick="Button2_Click" />                
              </span></td>
            </tr>
          </tbody>
        </table>
    </div>
后台:
public partial class Admin_Insert_group : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"].ToString() == "" || Session["UserID"].ToString() == null)
        {
            Response.Write("<script language=javascript>window.alert('为了系统安全,请您重新登陆');window.location.href=('Admin_Login.aspx')</script>");
        }
    }    protected void SButton_Click(object sender, EventArgs e)
    {
        string name;
        string workgroup;
        int groupid;
       
        name = Request["bzname"].ToString();
        
        workgroup = DropDownList1.SelectedItem.Text.ToString();
        groupid = int.Parse(DropDownList1.SelectedItem.Value.ToString());
        string banstr;
        banstr = ConfigurationManager.ConnectionStrings["myconnstring"].ToString();
        SqlConnection bancon = new SqlConnection(banstr);
        bancon.Open();
        SqlCommand bancmd = new SqlCommand();
        bancmd.CommandType = CommandType.Text;
        bancmd.Connection = bancon;
        string bansql;
        bansql = "INSERT INTO BZ_all VALUES('" + name + "','" + workgroup + "','" + groupid + "')";
        bancmd.CommandText = bansql;
        try
        {
            bancmd.ExecuteNonQuery();
            Literal txtMsg = new Literal();
            txtMsg.Text = "<script>alert('添加成功!')</script>";
            Page.Controls.Add(txtMsg);
            this.FindButton(this);
        }
        catch
        {
            Literal txtMsg = new Literal();
            txtMsg.Text = "<script>alert('添加失败!')</script>";
            Page.Controls.Add(txtMsg);
        }
        finally
        {
            bancon.Close();
        }    }
    private void FindButton(Control c)
    {
        if (c.Controls != null)
        {            foreach (Control x in c.Controls)
            {
                if (x is TextBox)
                {
                    ((TextBox)x).Text = "";
                }
                FindButton(x);
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.FindButton(this);    }
}更改DropDownList中的值之后就显示插入失败,菜鸟 找不到原因。

解决方案 »

  1.   

    很怀疑是不是没有判断回发哦,ispostback
      

  2.   

    workgroup = DropDownList1.SelectedItem;
    groupid = int.Parse(DropDownList1.SelectedValue);
      

  3.   

    程序没错,就是选择dropdownlist的除第一项意外的所有项  插入不成功 第一项就能插入成功。
      

  4.   


    //在bansql 設斷點,看下bansql是什麼,在數據庫執行,看下就知道了
    bancmd.CommandText = bansql;
      

  5.   

    <div>
            <br />
            <br />
            <table class="tableBorder" cellspacing="1" cellpadding="3" align="center" border="0">
              <tbody>
                <tr>
                  <th class="tableHeaderText" height="25">&nbsp;</th>
                </tr>
              </tbody>
              <tbody>
                <tr>
                  <td height="23" class="forumRow"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
                    <tr>
                      <td width="36%" align="right"><span style="width: 94px; height: 23px">班组名称:</span></td>
                      <td width="64%"><span style="height: 23px; width: 281px;">
                        <input name="bzname" type="text" value="总装车一般" id="bzname" style="width:144px;" />
                      &nbsp;
                          </span></td>
                    </tr>
                  </table></td>
                </tr>
                <tr>
                  <td height="23" class="forumRow"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
                    <tr>
                      <td width="36%" align="right"><span style="width: 94px">所属车间:</span></td>
                      <td width="64%">
                          <select name="DropDownList1" id="DropDownList1">
    <option value="1">加工车间</option>
    <option selected="selected" value="2">总装一车间</option>
    <option value="3">总装二车间</option>
    <option value="4">试车车间</option>
    <option value="5">成套车间</option>
    <option value="6">缸盖调试组</option>
    <option value="7">缸体调试组</option></select>
                          <span style="width: 281px">
                      &nbsp;&nbsp;&nbsp;
                      </span>
                        </td>
                    </tr>
                  </table></td>
                </tr>
               
                <tr>
                  <td height="23" align="center" class="forumRow"><span style="height: 24px">
                    <input type="submit" name="SButton" value="添加" id="SButton" style="height:23px;" />                
                    <font
                            face="宋体"> &nbsp;</font>
                    <input type="submit" name="Button2" value="取消" id="Button2" />                
                  </span></td>
                </tr>
              </tbody>
            </table>
        </div>
        
        </form>
    </body>
    </html><script>alert('添加失败!')<
    这是逐语句显示的结果。。
      

  6.   

    肯定是回发问题导致的,ispostback判断一下
      

  7.   

    你描述的问题,不是很明白,是插入时出现错误,还是插入时不能插入修改后的值?
    如果插入时出现错误,肯定是sql的问题,如果插入时不能插入修改后的值,
    1.你看一下这段代码是否取到了正确的值
     workgroup = DropDownList1.SelectedItem.Text.ToString();
      groupid = int.Parse(DropDownList1.SelectedItem.Value.ToString());
      是不是应该dropdownlist1.selecevalue;