?
Solved

Databases in aspx web pages using C#

Posted on 2003-03-16
8
Medium Priority
?
208 Views
Last Modified: 2010-04-15
Hi,

As regards web development, can someone please post some code (in C#) to open a connection to a database and print out the contents of it (assuming a database of any name with any contents - the SQL involved is not the concern).

I am learning asp.net with the language used being C# and can only find examples doing the above using VB as the language on the Internet.

I think the code will probably have to go in the Page_Load method, but correct me if I am wrong.

Cheers in advance.
0
Comment
Question by:mrwad99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Expert Comment

by:DiRN
ID: 8147040
Here is the code that I use to populate a menu on my Web site.  Each entry in the database will appear formatted the same way.  If you need more control over that, let me know and I'll post that code for you.

################################################

<%
string sMenuConn = "PROVIDER=Microsoft.Jet.OleDb.4.0;DATA SOURCE=" + Server.MapPath ("/databases/menu.mdb");
string MenuSQL = "SELECT * FROM Menu WHERE Disabled=False ORDER BY Menu_Item";
OleDbConnection MenuConn = new OleDbConnection (sMenuConn);
OleDbCommand MenuCmd = new OleDbCommand (MenuSQL, MenuConn);
MenuConn.Open ();

MenuRepeater.DataSource = MenuCmd.ExecuteReader (CommandBehavior.CloseConnection);
MenuRepeater.DataBind ();

MenuConn.Close ();
%>
<asp:repeater id="MenuRepeater" runat="server">
  <itemtemplate>
&nbsp; &nbsp; &nbsp;<a href="/<%# ((IDataRecord) Container.DataItem) ["Link"] %>"><%# ((IDataRecord) Container.DataItem) ["Menu_Item"] %></a><br>
  </itemtemplate>
</asp:repeater>

################################################

The code in the <% %> tags should go in the Page_Load method, while the <asp:repeater> tags should go where ever you need the code to appear.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8151161
DiRN for some reason I cannot get that code to work.  Can you alter it so that instead of outputting to a menu the data is output to a table.

Also why have you used OleDbConnection instead of SqlConnection ? All the examples I have use the latter which is why I am getting confused since I do not understand the parameters,

eg in my C# book I have:

SqlConnection conn = new SqlConnection("data source = localhost; userid =; password=;Initial Catalog=northwind");

 - Where is the database source specified ????

This then goes on to talk about DataSets and SqlAdapters which is even more confusing....

I am really getting
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8151173
...quite down about all this since I know exactly what I want to do but just cannot find the resources to tell me how to do it !

I have however figured out how to add data to the database and was relieved when it actually updated the .mdb file, but this is still a long way from outputting it and deciding on which of the many methods (as outlined above) I am going to use !

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:mrwad99
ID: 8151209
The exact error I got on the code you posted is:

Line 19:     MenuRepeater.DataSource = MenuCmd.ExecuteReader (CommandBehavior.CloseConnection);

System.Data.OleDb.OleDbException: No value given for one or more required parameters.
0
 

Accepted Solution

by:
DiRN earned 100 total points
ID: 8152296
I am not running MSSQLServer, which is why I use the OleDbConnection.  I use Windows' built in ODBC.  I've never actually done any work with Northwind, but I believe its samples use SQLServer.

The string

"PROVIDER=Microsoft.Jet.OleDb.4.0;DATA SOURCE=" + Server.MapPath ("/databases/menu.mdb")

specifies the data source in the Server.MapPath method.  So if, for example, your database was called "db.mdb" and it was stored in the same directory as your .aspx file, you would use Server.MapPath("db.mdb") to specify your data source.  Also, you should note that this is a DSN-less connection.

Here is some modified code:

<%
string sConn = "PROVIDER=Microsoft.Jet.OleDb.4.0;DATA SOURCE=" + Server.MapPath (<string containing path to database>);
string SQL = "<sql statement>";
OleDbConnection Conn = new OleDbConnection (sConn);
OleDbCommand Cmd = new OleDbCommand (SQL, Conn);
Conn.Open ();

Repeater.DataSource = Cmd.ExecuteReader (CommandBehavior.CloseConnection);
Repeater.DataBind ();

Conn.Close ();
%>

<table>
<asp:repeater id="Repeater" runat="server">
 <itemtemplate>
<tr>
<td><%# ((IDataRecord) Container.DataItem) ["Field1"] %></td>
<td><%# ((IDataRecord) Container.DataItem) ["Field2"] %></td>
</tr>
</itemtemplate>
</asp:repeater>
</table>

The output here has been setup for a table.  And try running your scripts with just the OleDbConnection, and not the SqlConnection.

I hope this helps.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8152326
Thank you DiRN,

I will look at that code and come back with any questions before awarding you with the points for your efforts.

Cheers again.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8165859
Worked like a treat DiRN; thank you profusely.

:-)
0
 

Expert Comment

by:DiRN
ID: 8167103
Glad to help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question