SQLConnection - probably simple

R

Robert Muchacki

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
 
J

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=username;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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top