By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,330 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,330 IT Pros & Developers. It's quick & easy.

SQLConnection - probably simple

P: n/a
Hello all!

I'm fighting with this little problem. I'm trying to retrieve data from a
table in SQL Server 2k. I'm dead. The only result I get is retrieving the
headers of the columns - no data in it actually :/ I'm probably missing
something on the way. Could somebody explain me how do I get data and even
WHERE I should retrieve it to? Explain like to an idiot - really.

Greets,

MuchaR
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
JPM
Robert,
Don't know 'what' exactly you want to display, but below is a sample of how you can display
data from an SQL Table in a "DataGrid" using ASP.NET (written in C#).

<!-------------------------------BEGIN EXAMPLE------------------------------------------>
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {

// Databind the data grid on the first request only
// (on postback, rebind only in paging and sorting commands)

BindGrid();
}
}

void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {

DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}

void DataGrid_Sort(object sender, DataGridSortCommandEventArgs e) {

DataGrid1.CurrentPageIndex = 0;
SortField = e.SortExpression;
BindGrid();
}

//---------------------------------------------------------
//
// Helpers
//
// use a property to keep track of the sort field, and
// save it in viewstate between postbacks
protected String SortField {

get {
object o = ViewState["SortField"];
return (o == null) ? String.Empty : (String)o;
}
set {
ViewState["SortField"] = value;
}
}

void BindGrid() {

// TODO: update the ConnectionString values for your application
string ConnectionString =
"server=ServerName;database=DatabaseName;UID=usern ame;Password=password";
string CommandText;

// TODO: update the CommandText values for your application
if (SortField == String.Empty)
CommandText = "select Column1, Column2, Column3, Column4, Column5 from TableName
order by Column1";
else
CommandText = "select Column1, Column2, Column3, Column4, Column5 from TableName
order by " + SortField;

SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Database example of ASP.NET DataGrid w/Paging & Sorting
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None"
CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page"
PageSize="6" AllowPaging="true" OnSortCommand="DataGrid_Sort" AllowSorting="true">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<PagerStyle horizontalalign="Right" backcolor="#C6C3C6"
mode="NumericPages"></PagerStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</form>
</body>
</html>

//--------------------------END EXAMPLE HERE------------------------------------------------>

NOTE: Copy the code into notepad, then save the file with a ".aspx" extension. The example is
VERY basic, but should give you a beginning point!

HTH

JPM

"Robert Muchacki" <mu******@go2.pl> wrote in message news:be**********@nemesis.news.tpi.pl...
Hello all!

I'm fighting with this little problem. I'm trying to retrieve data from a
table in SQL Server 2k. I'm dead. The only result I get is retrieving the
headers of the columns - no data in it actually :/ I'm probably missing
something on the way. Could somebody explain me how do I get data and even
WHERE I should retrieve it to? Explain like to an idiot - really.

Greets,

MuchaR

Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.