Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
plz help me tha how i can show the record of users in grid view when he login,i want to show the contacts numbers of user in grid view on page load event ,i need coding,
my coding is given below
C#
if (Session["FullName"] != null)
        {
            Response.Redirect("~/Account/Login.aspx");
            String UserId = Session["FullName"].ToString();
            try
            {   con.Open();
                string sql = "Select Name,ContactNo,Email from UserContacts Where User_id= '"+UserId+"'";
                cmd = new SqlCommand(sql, con);
                DataSet ds = new DataSet();
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                dataAdapter.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }
Posted
Updated 30-Jun-13 23:36pm
v2
Comments
_Amy 1-Jul-13 5:48am    
Why you are redirecting the page before binding the grid? Comment Response.Redirect function. It'll work.
Thanks7872 1-Jul-13 6:00am    
My bad. I didnt check whole codeblock and thought he just wanted to get it from database at the time of binding the grid. I removed the incorrect one.

1 solution

if (Session["FullName"] != null)
{
Response.Redirect("~/Account/Login.aspx");//May I know why you redirecting here
String UserId = Session["FullName"].ToString();
try
{ con.Open();
string sql = "Select Name,ContactNo,Email from UserContacts Where User_id= '"+UserId+"'";
cmd = new SqlCommand(sql, con);
DataSet ds = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(ds);
GridView1.DataSource = ds;//Here it must be "ds.tables(0)" based on number of tables content in dataset.
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
}
}
}
 
Share this answer
 

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