Hi
i have a requirement , i want to display the users in a webpart.
i have done some coding but it is not working.
i want to display the data from sql server table to a grid view in vs2010 through a web part
my code is
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data Source=spsrv;Initial Catalog=GrandCacheCodeDB;User Id=sa;Password=Pass1234;Integrated Security=True");
con.Open();
string str = ("select UserId,UserName from aspnet_Users ");
cmd = new SqlCommand(str, con);
da = new SqlDataAdapter(cmd);
da.Fill(ds, "aspnet_Users");
gv.DataSource = ds.Tables[0];
gv.DataBind();
con.Close();
}
}
}
the data is not geting binded to the gridview can u halp me out
Loading
Satyapriya NayakPosted May 4, 2012, 11:32 PM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Gridview._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Gridview
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
bindgrid();
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
g1.DataMember = "employee";
g1.DataSource = ds;
g1.DataBind();
con.Close();
}
}
}
Thanks