错误:指定的强制转换无效

本文关键字:转换 无效 错误 | 更新日期: 2023-09-27 18:33:31

嗨,程序员,我

尝试了一些东西,但它不起作用,所以我想我可能会在这里得到我想要的答案

我有一个 SearchUser.aspx 页面,其中有一个文本框、按钮和网格视图,当我转到该页面时,当我输入用户名并单击按钮时,我可以使用编辑和取消按钮查看所有用户的详细信息,我获取用户的详细信息,当我单击编辑链接时,我可以编辑用户的详细信息,但是当我单击搜索用户页面并单击在网格视图的编辑链接上,我得到

Specified cast is not valid. error

这是我在网格视图中遇到错误的地方

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

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

你们能帮我吗

提前感谢!!

错误:指定的强制转换无效

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

更新

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