Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here the code in classic asp, i want to do this in asp.net. I am using datalist in asp.net. How can i do this. Please help

ASP.NET
<%
  set con=Server.CreateObject("ADODB.Connection")
  con.Open strcon
  set rs=Server.CreateObject("ADODB.Recordset")
  rs.Open "SELECT * FROM category where article_allow=1",con,0,1 
			
			
  do while not rs.eof
    article_visit=0
				
    set rst=Server.CreateObject("ADODB.Recordset")
    rst.Open "SELECT article_visit FROM article_list where cat_ID="&rs("cat_ID")&" and article_status=1",con,0,1 
				
      do while not rst.eof
        article_visit=rst("article_visit")+article_visit
        rst.MoveNext
      loop	
      set rst=nothing
%>

Thanks in advance

Please provide me the code in c#


I convert the classic asp code to C# like this but it gives the result is 0, please check this code:
string article_visit="0";
SqlConnection con=Database.GetConnection();

DataTable data=new DataTable();
SqlCommand sqlQuery=new SqlCommand("select * from category where article_allow = 1", con);
SqlDataAdapter dataAdapter=new SqlDataAdapter(sqlQuery);
//Dim dataAdapter As New OdbcDataAdapter(sqlQuery, connectionString)
dataAdapter.Fill(data);
//Dim index As Integer = 0
int index=0;
do
{
article_visit="0";
SqlCommand cmd=new SqlCommand("SELECT article_visit FROM article_list where cat_ID=@cat_id and article_status=1",con);
cmd.Parameters.AddWithValue("@cat_id", data.Rows[index]["cat_ID"]);
dataAdapter=new SqlDataAdapter(cmd);
DataTable data1=new DataTable();
dataAdapter.Fill(data1);
int index1=0;
do
{
article_visit=int.Parse(data1.Rows[index1]["article_visit"].ToString()) + article_visit;
index1=index1+1;
}while(index1<data1.Rows.Count);
index=index+1;
}while(index<data.Rows.Count);
Posted
Updated 19-Jun-11 19:09pm
v4

this MSDN[^] article is a good place to start.

It covers most of your requirements
 
Share this answer
 
The same thing you can do in ASP.NET by following below listed steps which written in VB.NET:

1) If you are doing this process when your page is loading then go to page_load event and write below code in that OR write below code in appropriate event where you want

2) add name Namespace at top of the class file
VB
Imports System.Data.Odbc


3) Copy below code and test it as per your requirement

VB
Dim article_visit As Integer = 0
Dim connectionString As String = "Your connection string"
Dim data As DataTable = New DataTable()
Dim sqlQuery As String = "SELECT * FROM category where article_allow=1"
Dim dataAdapter As New OdbcDataAdapter(sqlQuery, connectionString)
dataAdapter.Fill(data)
        Dim index As Integer = 0
        Do
            article_visit = 0
            sqlQuery = "SELECT article_visit FROM article_list where cat_ID=" & data.Rows(index)("cat_ID") & " and article_status=1"
            dataAdapter = New OdbcDataAdapter(sqlQuery, connectionString)
            Dim data1 = New DataTable()
            dataAdapter.Fill(data1)
            Dim index1 As Integer = 0
            Do
                article_visit = Integer.Parse(data1.Rows(index1)("article_visit")) + article_visit
                index1 = index1 + 1
            Loop
            While index1 < data1.Rows.Count
            End While
            index = index + 1
        Loop
        While index < data.Rows.Count
        End While
 
Share this answer
 
Dim con As New System.Data.OleDb.OleDbConnection
        Dim cmd As New OleDbCommand("seelct * from category where article_allow = 1", con)
        Dim rs, rst As OleDbDataReader

        Dim article_visit As String = "0"
        con.Open()
        rs = cmd.ExecuteReader()

        While (rs.Read())
            Dim cmdrst As New OleDbCommand("SELECT article_visit FROM article_list where cat_ID=" & rs("cat_ID") & " and article_status=1", con)
            rst = cmdrst.ExecuteReader()
            While (rst.Read())
                article_visit = rst("article_visit") + article_visit
            End While
            //close datareader object
            rst.Close()
            rst = Nothing

            //dispose command object
            cmdrst.Dispose()
            cmd = Nothing
        End While
        rs.Close()
        rs = Nothing


hope it helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900