错误:指定的演员表无效

时间:2013-10-01 12:35:47

标签: c# asp.net gridview checkbox

你好编码器我尝试了一些东西,但它没有用,所以我想我可能会得到我想要的答案

我有一个searchuser.aspx页面,其中我有一个文本框,按钮和gridview,当我去那个页面时,我可以查看所有用户的详细信息,当我输入用户的名字并点击按钮i时,编辑和检测按钮获取用户的详细信息,当我点击编辑链接时,我可以编辑用户的详细信息但是当我点击搜索用户页面并点击gridview的编辑链接时我得到

Specified cast is not valid. error

这是我在gridview中出错的地方

 <asp:TemplateField HeaderText="IsEnable?">
                  <EditItemTemplate>
                        <asp:CheckBox ID="chkIsEnableEdit" runat="server" Checked='<%# Bind("Enable") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkIsEnable" runat="server" Checked='<%# Bind("Enable")%>' Enabled="False" />// here i got this error
                    </ItemTemplate>
                </asp:TemplateField>

我使用checked='%#<Convert.ToBoolean(Eval(Enable)%'> 但它不起作用

这是代码背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.GetGridData();
    }
}
protected void BindGrid()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Select * from CreateUser where FirstName like'"+tbSearchUser.Text+"'", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    else
    {
        ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
        GridView1.DataSource = ds;
        GridView1.DataBind();
        int columncount = GridView1.Rows[0].Cells.Count;
        GridView1.Rows[0].Cells.Clear();
        GridView1.Rows[0].Cells.Add(new TableCell());
        GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
        GridView1.Rows[0].Cells[0].Text = "No Records Found";
    }
}
private void GetGridData()
{
    con.Open();
    string query = "Select * from CreateUser";
    da = new SqlDataAdapter(query, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    con.Close();
}
protected void btnSearchUser_Click(object sender, EventArgs e)
{
    string query = "Select * from CreateUser where FirstName like'" + tbSearchUser.Text + "'";
    da = new SqlDataAdapter(query, con);
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    //this.BindGrid();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GetGridData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindGrid();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
    string query = string.Empty;
    string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString();
    TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox;
    TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
    TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox;
    TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox;
    TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox;
    TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox;
    DropDownList RoleType = GridView1.Rows[e.RowIndex].FindControl("ddlrole") as DropDownList;
    CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox;

    GridView1.EditIndex = -1;
    con.Open();

    SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con);
    cmd.ExecuteNonQuery();
    con.Close();
    BindGrid();
    //GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindGrid();
}
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
    string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString();
    try
    {
        con.Open();
        cmd.CommandText = "Delete FROM CreateUser where UserID='"+userid+"'";
        cmd.ExecuteNonQuery();
        con.Close();
        BindGrid();
        lblMessage.Text = "Record Deleted successfully.";
        // Refresh the data
        BindGrid();
    }
    catch (SqlException ee)
    {
        lblMessage.Text = ee.Message;
    }
    finally
    {
        cmd.Dispose();
        con.Close();
        con.Dispose();
      }
   }
}

enable是数据库中的位。当我在我的数据库中将我的位值更改为bool时,我将无法获得该数据类型,我不知道为什么

你们可以帮帮我吗

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

根据你的评论,该列可以保存空值(这对于真/假条件不是理想的):

<强>已更新

Checked='<%# Eval("Enable").GetType() != typeof(DBNull) ? Convert.ToBoolean(Eval("Enable")) : false %>'