473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SQLConnection - probably simple

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
1 4104
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
1
by: Mark | last post by:
I have an SqlConnection object defined as a member of main form. I also have a thread that creates an SqlDataReader but uses the same SqlConnection object. The SqlDataReader simple loops round for...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
2
by: MrMike | last post by:
I have a web.config file containing the SQLConnection String to a SQL Database. Before I implemented this SQLConnection String into web.config, I had individual SQLConnection objects in each...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
2
by: RvGrah | last post by:
My program is giving a strange exception at startup, only on client machines. Runs fine on development machine. The app was upgraded long ago from a 2003 version to a VS2005 version, but continued...
4
by: =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?= | last post by:
Hello, I am planning to use three databases on the local SQL Server. DB1 will be for App1, DB2 for App2, and DB3 will hold tables, that both, App1 and App2 need to access. With applications...
3
bencoding
by: bencoding | last post by:
Wow, I've spent about 3 hrs now trying to figure this one out, none of the articles online were able to help me and some were using system libraries that were obsolete......anyways, I am able to...
1
by: Tony Johansson | last post by:
Hello! Below is a simple program that works fine. In this program there is no explicit open for the SqlConnection instead the DataAdapter will open the Connection to the database automatically....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.