473,700 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4121
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="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.SqlClie nt" %>
<script runat="server">

void Page_Load(objec t sender, EventArgs e) {

if (!Page.IsPostBa ck) {

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

BindGrid();
}
}

void DataGrid_Page(o bject sender, DataGridPageCha ngedEventArgs e) {

DataGrid1.Curre ntPageIndex = e.NewPageIndex;
BindGrid();
}

void DataGrid_Sort(o bject sender, DataGridSortCom mandEventArgs e) {

DataGrid1.Curre ntPageIndex = 0;
SortField = e.SortExpressio n;
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 ConnectionStrin g values for your application
string ConnectionStrin g =
"server=ServerN ame;database=Da tabaseName;UID= username;Passwo rd=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(C onnectionString );
SqlDataAdapter myCommand = new SqlDataAdapter( CommandText, myConnection);

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

DataGrid1.DataS ource = ds;
DataGrid1.DataB ind();
}

</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="Whit e" ForeColor="Blac k" OnPageIndexChan ged="DataGrid_P age"
PageSize="6" AllowPaging="tr ue" OnSortCommand=" DataGrid_Sort" AllowSorting="t rue">
<HeaderStyle font-bold="True" forecolor="whit e" backcolor="#4A3 C8C"></HeaderStyle>
<PagerStyle horizontalalign ="Right" backcolor="#C6C 3C6"
mode="NumericPa ges"></PagerStyle>
<ItemStyle backcolor="#DED FDE"></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.p l> 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
13985
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 Application that connects to a MSDE Database. I was reading the thread yesterday on this very same subject, but it only mentioned ASP.NET Applications. Can anyone tell me if it's a good idea
1
2390
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 each row in the result set. The problem I have is that after running the thread many times, the SqlConnection object simply closes the connection to the database (picked up from the StateChange event). No exceptions are generated. Sometimes the...
11
2890
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 reduces the need to getting and returning connections to the pool repeatedly if a page has multiple calls to the DB, and each one manages its own connection. However, this does requires more deliberate coding, like calling the...
2
1198
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 webform's designer tray. I am now going back through the webforms and replacing the individual SQLConnection Objects with a pointer to web.config ConnectionString. However, whenever I delete the SQLConnection String objects from each webform, they...
4
2054
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 thing, then when everything is rendered down to the browser, and all references to the page object are ended, the connection to the server can be closed. First of all, are the SqlConnection Open and Close methods quick performers and I dont need to...
2
3404
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 to run fine until just a couple of weeks ago. Now new builds give the error when the code hits: SqlConnection cn = new SqlConnection(); The error is: System.TypeInitializationException: The type initializer for...
4
1697
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 I mean ASP.NET applications.
3
1644
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 read my connection string from my web.config file withing a Response.Write(); but when I put it in my SqlConnection() it won't display, I don't get why Can someone PLEASE I'm begging for a simple solution for this, please help! This works...
1
1601
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. Now to my question if I check the connection after the SqlDataAdapter has been created the connection is then closed ? So when does the SqlDataAdapter open and close the DataAdapter ?
0
8725
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8644
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4656
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3088
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.