Help with some ado/asp to .Net conversion

ussfletcher

Platinum Member
Apr 16, 2005
2,569
2
81
So basically, I am translating a bunch of asp programs to asp.net, and I think I have figured pretty much everything out except for how to replace these ADODB Recordsets with something inherently supported in the .Net framework, datasets I suppose. I have been looking at this for hours and for some reason I just can't get it to work out, even with the help of some online tutorials.

Here is the original code, if you guys could point me in the right direction I'd appreciate it

Code:
rsTeil = Server.CreateObject("ADODB.RecordSet")
    qryTeil = "select * from teil where teteil=" & teil
    rsTeil.Open(qryTeil, dbConn)
    
    If Not rsTeil.EOF Then
        teteilnr = rsTeil("teteilnr")
        tebezeich = rsTeil("tebezeich")
    End If
    partNum = teteilnr
    partDesc = tebezeich

    rsChartInfo = Server.CreateObject("ADODB.RecordSet")
    qryChartInfo = "select * from tFMKChart where iChartID=" & iChartID
    rsChartInfo.Open(qryChartInfo, dbConn)
    If Not rsChartInfo.EOF Then
        chartID = rsChartInfo("vcChartID")
        chartDesc = rsChartInfo("vcChartDesc")
        chInterval = rsChartInfo("chInterval")
        dtdateFrom = rsChartInfo("dtDateFrom")
        dtdateTo = rsChartInfo("dtDateTo")
        scope = rsChartInfo("vcChartScope")
        lastN = rsChartInfo("iLastN")
    End If

I suppose I should add that I have imported the ADODB namespace, and that lets the page load but everywhere that it would normally have content displayed it says system.__ComObject instead.
 
Last edited:

Hmongkeysauce

Senior member
Jun 8, 2005
360
0
76
I haven't done classic ASP at all but here is it a crack at it:

you'll need the namespace:

Code:
using System.Data;
using System.Data.SqlClient;

Then, to open the database connection and fill in it a dataset(in this case, I'm using a DataTable)

Code:
string qryTeil = "SELECT * FROM teil WHERE teteil = @teil";
string qryChartInfo = "SELECT * FROM tFMKChart WHERE iChartID = @iChartID";
DataTable rsTeil = new System.Data.DataTable();
DataTable rsChartInfo = new System.Data.DataTable();
SqlCommand rsteilCommand;
SqlCommand rsChartInfoCommand;
SqlDataAdapter rsAdapter;
SqlDataAdapter rsChartInfoAdapter;
            
using (SqlConnection con = new SqlConnection(connectionString))
{
    rsteilCommand = new SqlCommand(qryTeil, connectionString);
    rsteilCommand.Parameters.AddWithValue("@teil", teil);
    rsAdapter = new SqlDataAdapter(rsteilCommand);
    rsAdapter.Fill(rsTeil);

    rsChartInfoCommand = new SqlCommand(qryChartInfo, connectionString);
    rsChartInfoCommand.Parameters.AddWithValue("@iChartID", iChartID);
    rsChartInfoAdapter = new SqlDataAdapter(rsChartInfoCommand);
    rsChartInfoAdapter.Fill(rsChartInfo);
}

I used parameterized queries in the example above. You may, if you wish, continue to use string concatenation. You'll have to declare the variable connectionString somewhere before using it above. And assuming that you only retrieve one row of record, to access the values, you can do the following:

Code:
teteilnr = rsTeil.Rows[0]["teteilnr"];

Or if you want to iterate through the dataset(DataTable)
Code:
foreach (DataRow rows in rsTeil.Rows)
{
    qryTeil = rows["teteilnr"];
}

You may need to cast each column to the data type that you are expecting. I hope this helps.
 
Last edited:

ussfletcher

Platinum Member
Apr 16, 2005
2,569
2
81
I haven't done classic ASP at all but here is it a crack at it:

you'll need the namespace:

Code:
using System.Data;
using System.Data.SqlClient;
Then, to open the database connection and fill in it a dataset(in this case, I'm using a DataTable)

Code:
string qryTeil = "SELECT * FROM teil WHERE teteil = @teil";
string qryChartInfo = "SELECT * FROM tFMKChart WHERE iChartID = @iChartID";
DataTable rsTeil = new System.Data.DataTable();
DataTable rsChartInfo = new System.Data.DataTable();
SqlCommand rsteilCommand;
SqlCommand rsChartInfoCommand;
SqlDataAdapter rsAdapter;
SqlDataAdapter rsChartInfoAdapter;
            
using (SqlConnection con = new SqlConnection(connectionString))
{
    rsteilCommand = new SqlCommand(qryTeil, connectionString);
    rsteilCommand.Parameters.AddWithValue("@teil", teil);
    rsAdapter = new SqlDataAdapter(rsteilCommand);
    rsAdapter.Fill(rsTeil);

    rsChartInfoCommand = new SqlCommand(qryChartInfo, connectionString);
    rsChartInfoCommand.Parameters.AddWithValue("@iChartID", iChartID);
    rsChartInfoAdapter = new SqlDataAdapter(rsChartInfoCommand);
    rsChartInfoAdapter.Fill(rsChartInfo);
}
I used parameterized queries in the example above. You may, if you wish, continue to use string concatenation. You'll have to declare the variable connectionString somewhere before using it above. And assuming that you only retrieve one row of record, to access the values, you can do the following:

Code:
teteilnr = rsTeil.Rows[0]["teteilnr"];
Or if you want to iterate through the dataset(DataTable)
Code:
foreach (DataRow rows in rsTeil.Rows)
{
    qryTeil = rows["teteilnr"];
}
You may need to cast each column to the data type that you are expecting. I hope this helps.
That is very helpful, thanks a ton.