Usage of DataView in my custom DataBase Class

I make one database class, in which there is a dataView method. In the aspx file, this method was invoked with the purpose to display the data in database. But here the aspx file failed to display the data, not even the error message in the catch block.

Can anybody tell me why

cs file:

using System;
using System.Data;
using System.Data.SqlClient;

namespace database
{
public class DBClass
{

private SqlConnection conn;
private SqlCommand comm;
public DataSet ds;
public SqlDataAdapter dad;
public DataView dv;
private string sql;
private string connStr;
private string errInfo;

public void database()
{
errInfo = "";
}

public string ErrInfo
{
get {return errInfo;}
}
public string Sql
{
get {return sql;}
set {sql=value;}
}
public string ConnStr
{
get {return connStr;}
set {connStr=value;}
}

private void connDb()
{
conn = new SqlConnection(connStr);
try
{
conn.Open();
}
catch(SqlException e)
{
for(int i=0;i<e.Errors.Count;i++)
{
errInfo+="Error Sequential Number:" +i+ "\n" + "Error Info:" +e.ErrorsIdea.Message+ "\n" + "Error Source:" +e.ErrorsIdea.Source+ "\n" + "Program:" +e.ErrorsIdea.Procedure;
}
conn.Close();
}

}

public void dataView()
{
connDb();
dad = new SqlDataAdapter(sql,conn);
ds = new DataSet();
dad.Fill(ds,"table");
DataView dv = new DataView(ds.Tables["table"]);
}

public void clear()
{
conn.Close();

}

}

}

aspx file:

<%@ Page Language="C#" Debug="true"%>
<%@ Register TagPrefix="database" Namespace="database" Assembly="database"%>

<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
DBClass db = new DBClass();
try
{
db.ConnStr = "server=localhost;database=article;uid=sa;pwd=''";
db.Sql = "select * from article";
db.dataView();
dg.DataSource = db.dv;
dg.DataBind();
db.clear();
}
catch
{
Response.Write(db.ErrInfo);
}
}
</script>
<ASP:DataGrid id="dg" runat="server"/>




Answer this question

Usage of DataView in my custom DataBase Class

  • Susan Entwisle

    :)

    Excellent! You are right!

    After change, now it works very well.

    thks a lot.



  • Olivia Luo

    At first read I cannot see obvious problems. Are you sure there is data in article table in database

  • gsylvest

    you're right. There is problem in DataView method. you create new DataView ovject instead of use your class member. I missed it on first read

    try this:

    public void dataView()
      {
       connDb();
       dad = new SqlDataAdapter(sql,conn);
       ds = new DataSet();
       dad.Fill(ds,"table");
       dv = new DataView(ds.Tables["table"]);   // CHANGE IS HERE
      }

     

    Hope this helps



  • Steve4125

    Yes,I am very sure that there is data in the article sheet of article database.

    I feel that the dataview section has sth. wrong, but I don't know exactly what the problem is.



  • Usage of DataView in my custom DataBase Class