0

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

我有一个 searchuser.aspx 页面,其中有一个文本框、按钮和 gridview 当我转到该页面时,当我输入用户名并单击按钮时,我可以使用编辑和删除按钮查看所有用户的详细信息并单击按钮我得到详细信息用户的,当我单击编辑链接时,我可以编辑用户的详细信息,但是当我单击搜索用户页面并单击 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 时,我将无法获得该数据类型,我不知道为什么

你们能帮帮我吗

提前致谢!!!

4

1 回答 1

1

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

更新

Checked='<%# Eval("Enable").GetType() != typeof(DBNull) ? Convert.ToBoolean(Eval("Enable")) : false %>'
于 2013-10-01T13:00:18.830 回答