我已经设置了一个用户定义的连接字符串,现在尝试使用它来填充gridview,断点显示连接字符串很好..也没有错误,但令人惊讶的是datatable没有收到任何值。尽管数据存在..我做错了什么?
Default3.Aspx:
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
</asp:GridView>
</td>
Default3.Aspx.Cs:
protected void Page_Load(object sender, EventArgs e)
{
string con = OracleDatabase.connection();
String SelectCommand = "select * from EMP";
OracleDataAdapter adp = new OracleDataAdapter(SelectCommand, con);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
/*
OracleDatabase.gridpopulate(SelectCommand, GridView1);
GridView1.DataSource = OracleDatabase.gridpopulate(SelectCommand);
GridView1.DataBind();
*/
//i tried doing this by writting a function At class file also
}
类文件:
public static string connection()
{
oradb = ConfigurationManager.ConnectionStrings["ConnectionString"]
.ConnectionString;
string str = "Data Source="+db+";User ID="+userid+";Password="+password+";";
oradb = String.Concat(oradb, str);
con = new OracleConnection(oradb);
con.Open();
return oradb;
}
/*(function for gridpopulate)
public static void gridpopulate(string SelectCommand,GridView grid1)
{
string con =(ConfigurationManager.ConnectionStrings["ConnectionString"]
.ConnectionString);
con = String.Concat(con, str);
adp = new OracleDataAdapter(SelectCommand,con);
DataTable dt = new DataTable();
adp.Fill(dt);
grid1.DataSource = dt;
grid1.DataBind();
//return dt;
}
*/
答案 0 :(得分:1)
尝试按照以下方式,而不是从函数返回连接字符串返回Oracle Connection对象,并将其传递给Adapter。
OracleConnection con = OracleDatabase.connection();
public static OracleConnection connection()
{
oradb =ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str = "Data Source="+db+";User ID="+userid+";Password="+password+";";
oradb = String.Concat(oradb, str);
con = new OracleConnection(oradb);
con.Open();
return con;
}