无效的强制转换异常,asp.net c#

时间:2015-08-13 05:27:42

标签: c# asp.net

再来一次! 我以前想过的错误不是正确的错误。我使用了正确的方法来显示数据。但真正的问题是在我的asp.net页面上。 当我从数据库中获取数据时,有一些列是布尔值的! 当我尝试做的时候:

<td><asp:Checkbox ID="sup_lcked" runat="server" Checked='<%# Eval("sup_blocked") %>' /></td>

这就是我得到如此无效的演员阵容的地方。 而我不明白的是,数据库中同类型的其他列完美地工作,但这不是!

感谢

2 个答案:

答案 0 :(得分:1)

使用DataReader

SqlCommand cmd = new SqlCommand("Select facture, count(l.le_ville) as nbre from table group by l.le_ville", con);
SqlDataReader dr = cmd.ExecuteReader();

 if (dr .HasRows)
        {
            while (dr .Read())
            {
                string facture=Convert.toString(dr[0]);
                string nbre =Convert.toString(dr[1]);
            }
        }

使用DataAdapter

SqlCommand cmd = new SqlCommand("Select facture, count(l.le_ville) as nbre from table group by l.le_ville", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
      string facture=Convert.toString(dt.Rows[0][0]);
      string nbre =Convert.toString(dt.Rows[0][1]);
}

答案 1 :(得分:1)

根据评论中的建议,您应该使用ExecuteReaderDataAdapter方法。以下是一些例子:

ExecuteReader方法的用法

public class FactureInfo
{
    public int Facture {get; set;}
    public int Nbre {get; set;}
}

public IList<FactureInfo> GetFactureInfo()
{
    DbConnection conn = null;
    DbDataReader reader = null;

    try
    {
        conn = new OleDbConnection(
            "Provider=Microsoft.Jet.OLEDB.4.0; " + 
            "Data Source=" + Server.MapPath("MyDataFolder/MyAccessDb.mdb"));
        conn.Open();

        DbCommand cmd = 
            new OleDbCommand("select facture, count(l.le_ville) as nbre
                from table
                group by l.le_ville", conn);

        reader = cmd.ExecuteReader();

        var factures = new List<FactureInfo>();
        while(reader.Read())
        {
            var factureInfo= new FactureInfo();
            factureInfo.Facture = reader.GetInt32(reader.GetOrdinal("facture"));
            factureInfo.Nbre = reader.GetInt32(reader.GetOrdinal("nbre"));

            factures.Add(factureInfo);
        }

        return factures;
    }
    finally
    {
        if (reader != null)  reader.Close();
        if (conn != null)  conn.Close();
    }

    return null;
}

DataAdapter方法的用法

public DataSet GetFactureInfo()
{
    DbConnection conn = null;
    DataSet dataSet = null;

    try
    {
        conn = new OleDbConnection(
            "Provider=Microsoft.Jet.OLEDB.4.0; " + 
            "Data Source=" + Server.MapPath("MyDataFolder/MyAccessDb.mdb"));
        conn.Open();

        DbDataAdapter dataAdapter = 
            new OleDbDataAdapter("select facture, count(l.le_ville) as nbre
                from table
                group by l.le_ville", conn);

        dataSet = new DataSet();
        dataAdapter.Fill(dataSet);

        return dataSet;
    }
    finally
    {
        if (dataAdapter != null)  dataAdapter.Dispose();
        if (conn != null)  conn.Dispose();
    }

    return null;
}

请注意,在示例中我使用了Access数据库和OLEDB连接,但是这对其他类型的数据库也是如此。只需更改连接字符串并实例化正确类型的类(例如,SqlConnection,SqlCommand,SqlDataReader和SqlDbAdapter以连接到MS SQL Server数据库)。