从sql server 2008数据库获取数据到datagrid [英] Fetching data into datagrid from sql server 2008 database
本文介绍了从sql server 2008数据库获取数据到datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在sql server中有一个数据库。我想在asp.net的datagrid中从中获取值
我使用了以下代码。每次我运行页面时都会显示找不到记录。
i已在此处使用的表中输入了一些条目,但它没有显示输出。
im asp.net的新用户请帮助我...
i have a database in sql server. and i want to fetch values from it in the datagrid in asp.net
i have use the following code. every time i run the page it shows "no Records found".
i have entered some entries in the table used here but it is not showing the output.
i m new to asp.net pls help me...
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Server=dimts-022;Initial Catalog=db_asset;User=UserName;password=Password");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from b_asset_master", con);
DataTable ds = new DataTable();
da.Fill(ds);
Grid.DataSource = ds;
Grid.DataBind();
}
catch
{
Label1.Text = "No records Found";
Label1.Visible = true;
}
finally
{
con.Close();
}
}
}
推荐答案
你正在点击你的catch块这一事实表明您在BindData()的try部分中遇到代码问题。您的数据库连接是否正常打开?您是否指定了正确的用户名和密码?数据库db_asset是否包含表/视图b_asset_master?
The fact that you are hitting your catch block indicates that you have a problem with the code in the try section of BindData(). Is your database connection opening properly? Have you specified the correct username and password? Does the database db_asset contain the table/view b_asset_master?
这篇关于从sql server 2008数据库获取数据到datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文