[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1063
  • Last Modified:

Trying to learn C# and Web Service

Okay...I'm trying to learn something new and having problems.
I want to write a webservice and use an aspx file to read it.  All files will be located on my 'work' web server,  My database is in MS Access 2005.  I have written my asmx file and compiled it okay with wsdl to get my cs file, and used csc to convert that to DLL.  So, I am able to call my asmx file and get the method listing.

But,

I am trying to read the Access db using an OLEdb, and NOT Sql C# syntax.  Is this correct?

I think that using a datagrid might be a problem.

Any ideas?  Help...an explanation.....PPLLEEAASSEE (whiny voice)

Bob

my ASMX file
===========
 
<%@ WebService Class="BeerServiceDB" Language="C#" %>
using System;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
 
[WebService( Namespace="http://web2.bender.com/rkoch1/webservices" )]
 
public class BeerServiceDB : WebService
{
    [WebMethod(Description="This method returns the 'Beer' catalog.")]
        public DataSet processQuoteRequest() {
            string strSelect = "Select ProductName, ProductDescription, UnitPrice, filepath From Beers;";
            OleDbConnection myConnection = new OleDbConnection "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../Database/KochBeer.mdb");
           OleDbCommand myCommand = new OleDbCommand(strSelect, myConnection);
           OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myCommand);
           DataSet myDataSet = new DataSet();
           myDataAdapter.Fill(myDataSet, "Beers");
           return myDataSet;
     }
}
 
 
Attempted ASPX file to read and send back a DataSet with the data.
=======================================================
<%@ Page Language="C#" %>
<script language="C#" runat=server>
 
void Page_Load(Object sender , EventArgs e) {
     BeerServiceDB objBeers = new BeerServiceDB();
     dgrdBeers.DataSource = objBeers.processQuoteRequest();
     dgrdBeers.DataBind();
}
</Script>
 
<html>
<head><title>DisplayBeerCatalog.aspx</title></head>
<body>
 
<h2>Available Beers:</h2>
<asp:DataGrid  ID="dgrdBeers"  Runat="Server"/>
 
</body>
</html>

Open in new window

0
Bob Bender
Asked:
Bob Bender
  • 2
1 Solution
 
Anurag AgarwalPython DeveloperCommented:
<<<I am trying to read the Access db using an OLEdb, and NOT Sql C# syntax.  Is this correct?
Yes this is correct
 
I guess in your code you forgot to open the connection, so your code
OleDbConnection myConnection = new OleDbConnection "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../Database/KochBeer.mdb");
           OleDbCommand myCommand = new OleDbCommand(strSelect, myConnection);
should be like this
 OleDbConnection myConnection = new OleDbConnection "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../Database/KochBeer.mdb");
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(strSelect, myConnection);

Also don't forgot to close the connection once data is fetched
Anurag
0
 
Anurag AgarwalPython DeveloperCommented:
sorry, i think i missed some code to read...
I beleive adding one attribute autogeneratecolumns=true will work for you in
<asp:DataGrid  ID="dgrdBeers"  Runat="Server"/>
 
Anurag
0
 
Bob BenderAuthor Commented:
A couple of other lines had to change, but it now works!  Thanx!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now