Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

C#
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();
        }
    }
}
Posted
Updated 25-Jun-13 20:37pm
v4

1 solution

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?
 
Share this answer
 
Comments
diyapoornima 26-Jun-13 2:29am    
i m using windows authentication in sql server... and for that i have provide my windows password. and it is correct. and yes db_asset contain table b_asset_master
_Damian S_ 26-Jun-13 2:46am    
You aren't using the correct connection string for windows authentication. Check out www.connectionstrings.com
diyapoornima 26-Jun-13 2:55am    
thanks a lot _Damian S_ ... :D
it worked...
PrahladMca 26-Jun-13 3:06am    
No need to open and close SQL connection with SQLDataAdapter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900