我的代码是在另一台电脑写的 我把程序过来放在公司的电脑上就报找不到数据库
我测试了连接串没有问题,不知道问题出在什么地方 后台的代码如下
namespace WebApplication1
{
public partial class test02 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.ListView1.AlternatingItemTemplate = new TempLate("AlternatingItemTemplate");
this.ListView1.EmptyDataTemplate = new TempLate("EmptyDataTemplate");
DataSet ResultSet = RunQuery("Select * From UserInfo");
ListView1.DataSource = ResultSet;
ListView1.DataBind();
} DataSet RunQuery(String QueryString)
{ // Declare the connection string. This example uses Microsoft SQL Server
// and connects to the Northwind sample database.
String ConnectionString = "Data Source=XU-AE42BC5C42AC;Initial Catalog=uzone;Persist Security Info=True;User ID=sa;Password=sa"; SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet(); try
{ // Run the query and create a DataSet.
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet); // Close the database connection.
DBConnection.Close(); }
catch (Exception ex)
{ // Close the database connection if it is still open.
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
} // Message.Text = "Unable to connect to the database."; } return ResultsDataSet; } public class TempLate : ITemplate
{
private string templateType = "";
public TempLate(string type)
{
this.templateType = type;
} //region ITemplate Members public void InstantiateIn(Control container)
{
Label UserIDLabel = new Label();
Label UserNameLabel = new Label();
Label GroupIDLabel = new Label();
UserIDLabel.ID="UserIDLabel";
UserIDLabel.Text = "<%# Eval(\"UserID\") %>";
UserNameLabel.ID="UserNameLabel";
GroupIDLabel.ID="GroupIDLabel";
switch (this.templateType)
{
case "AlternatingItemTemplate":
container.Controls.Add(new LiteralControl("<tr><td>"));
container.Controls.Add(UserIDLabel);
container.Controls.Add(new LiteralControl("</td><td>"));
container.Controls.Add(UserNameLabel);
container.Controls.Add(new LiteralControl("</td><td>"));
container.Controls.Add(GroupIDLabel);
container.Controls.Add(new LiteralControl("</td></tr>"));
break;
case "EmptyDataTemplate":
container.Controls.Add(new LiteralControl("<tr></tr>"));
break;
}
}
//endregion
}
}
}报这样的错误:The IListSource does not contain any data sources.
我测试了连接串没有问题,不知道问题出在什么地方 后台的代码如下
namespace WebApplication1
{
public partial class test02 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.ListView1.AlternatingItemTemplate = new TempLate("AlternatingItemTemplate");
this.ListView1.EmptyDataTemplate = new TempLate("EmptyDataTemplate");
DataSet ResultSet = RunQuery("Select * From UserInfo");
ListView1.DataSource = ResultSet;
ListView1.DataBind();
} DataSet RunQuery(String QueryString)
{ // Declare the connection string. This example uses Microsoft SQL Server
// and connects to the Northwind sample database.
String ConnectionString = "Data Source=XU-AE42BC5C42AC;Initial Catalog=uzone;Persist Security Info=True;User ID=sa;Password=sa"; SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet(); try
{ // Run the query and create a DataSet.
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet); // Close the database connection.
DBConnection.Close(); }
catch (Exception ex)
{ // Close the database connection if it is still open.
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
} // Message.Text = "Unable to connect to the database."; } return ResultsDataSet; } public class TempLate : ITemplate
{
private string templateType = "";
public TempLate(string type)
{
this.templateType = type;
} //region ITemplate Members public void InstantiateIn(Control container)
{
Label UserIDLabel = new Label();
Label UserNameLabel = new Label();
Label GroupIDLabel = new Label();
UserIDLabel.ID="UserIDLabel";
UserIDLabel.Text = "<%# Eval(\"UserID\") %>";
UserNameLabel.ID="UserNameLabel";
GroupIDLabel.ID="GroupIDLabel";
switch (this.templateType)
{
case "AlternatingItemTemplate":
container.Controls.Add(new LiteralControl("<tr><td>"));
container.Controls.Add(UserIDLabel);
container.Controls.Add(new LiteralControl("</td><td>"));
container.Controls.Add(UserNameLabel);
container.Controls.Add(new LiteralControl("</td><td>"));
container.Controls.Add(GroupIDLabel);
container.Controls.Add(new LiteralControl("</td></tr>"));
break;
case "EmptyDataTemplate":
container.Controls.Add(new LiteralControl("<tr></tr>"));
break;
}
}
//endregion
}
}
}报这样的错误:The IListSource does not contain any data sources.
String ConnectionString ="server=.;database=你的数据库;user id=sa;password=sa;min pool size=4;max pool size=512;";
ListView1.DataSource = ResultSet;
看看ResultSet是不是NULL
应该写在filly里边吧。