Solved

Convert c# to asp.net

Posted on 2011-03-03
5
1,185 Views
Last Modified: 2013-12-17
I am needing help with converting this c# application that i did to be an asp.net application.
using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Data;
	 
	using System.Data.SqlClient;
	 
	namespace ConsoleApplication1
	{
	    class Program
	    {
	        static void Main(string[] args)
	        {


string strConn, strSQL;
	            strConn = @"Data Source=. \SQLExpress; " +
	                       "Initial Catalog=Northwind;Integrated Security=True";
	            strSQL = "SELECT Customers.CustomerID, COUNT(Orders.OrderID) AS numOrders FROM Customers INNER JOIN Orders ON Customers.CustomerID  = Orders.CustomerID GROUP BY Customers.CustomerID";
	            DataSet ds = new DataSet();
	            SqlDataAdapter da = new SqlDataAdapter(strSQL, strConn);
	            da.Fill(ds);
	            //Display the customer information retrieved
	            foreach (DataRow row in ds.Tables[0].Rows)
	                Console.WriteLine("{0} {1}", row["CustomerID"], row["numOrders"]);
	 
	 
	 
	 
	            Console.Read();

}
}
}

Open in new window

0
Comment
Question by:hiddenjob
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35027937
You still can use the same code, only the displaying on the screen will be different (fe a datagrid)


http://www.aspnettutorials.com/tutorials/database/db-grid-aspnet2-csharp.aspx
http://msdn.microsoft.com/en-us/library/aa479341.aspx
string strConn, strSQL;
	            strConn = @"Data Source=. \SQLExpress; " +
	                       "Initial Catalog=Northwind;Integrated Security=True";
	            strSQL = "SELECT Customers.CustomerID, COUNT(Orders.OrderID) AS numOrders FROM Customers INNER JOIN Orders ON Customers.CustomerID  = Orders.CustomerID GROUP BY Customers.CustomerID";
	            DataSet ds = new DataSet();
	            SqlDataAdapter da = new SqlDataAdapter(strSQL, strConn);
	            da.Fill(ds);
gvwExample.DataSource = ds;

gvwExample.DataBind();

Open in new window

0
 

Author Comment

by:hiddenjob
ID: 35028022
So l will be able to use the code I had, with that slight change that you made with the gvwexample etc etc and be good to go?

Or I now need to make another file that will be calling this information through the datagrid

Programming is not my forte and you have to spell things out for me sometimes or i just get confused.
0
 

Author Comment

by:hiddenjob
ID: 35028539
So is this what I need to have done? It looks to be right but I cannot get it to run to test. Feedback?
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Project 4</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
a:link {
	color: #0000FF;
}
a:visited {
	color: #0000FF;
}
a:hover {
	color: #0000FF;
	text-decoration: none;
}
a:active {
	color: #0000FF;
	}
.basix {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
.header1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-weight: bold;
	color: #006699;
}
.lgHeader1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 18px;
	font-weight: bold;
	color: #0066CC;
	background-color: #CEE9FF;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" runat="server">
<br />
<table width="752" border="0" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#5482FC"> 
<td colspan="4"></td>
</tr>
<tr> 
<td width="1" bgcolor="#5482FC"></td>
<td width="250"></td>
<td width="500" bgcolor="#3399ff"></td>
<td width="1" bgcolor="#5482FC"></td>
</tr>
<tr bgcolor="#5482FC"> 
<td colspan="4"></td>
</tr>
</table>
<div><br />
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
<tr> 
<td height="50" align="center" class="lgHeader1">How to Display Data using the GridView control ASP.NET 
2.0 and C#</td>
</tr>
</table>
<br />
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr> 
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Database Results</td>
<td align="center" bgcolor="#FFFFFF">
<asp:GridView ID="gvwExample" runat="server" AutoGenerateColumns="False" CssClass="basix"  > 
<columns>
<asp:BoundField DataField="customersID" HeaderText="CustomerID"  />
<asp:BoundField DataField="numOrders" HeaderText="Order #'s" />

</columns>
</asp:GridView> 
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>
<br />
<table width="500" align="center" cellpadding="0" cellspacing="0">
<tr> 
<td height="50" align="center" class="basix"><br />

<br />
</td>
</tr>
</table>
<br />
<br />
<br />
</div>
</form>
</body>
</html>

Open in new window

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
     

        SqlCommand cmd = new SqlCommand("SELECT Customers.CustomerID, COUNT(Orders.OrderID) AS numOrders FROM Customers INNER JOIN Orders ON Customers.CustomerID  = Orders.CustomerID GROUP BY Customers.CustomerID", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));

     try
     {

      cmd.Connection.Open();

      gvwExample.DataSource = cmd.ExecuteReader();

      gvwExample.DataBind();

      cmd.Connection.Close();
      cmd.Connection.Dispose();

     }

     catch (Exception ex)
     {
      lblStatus.Text = ex.Message;
     }


    }
}

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35034115
If you debug it, do you get any error ?
0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 250 total points
ID: 35034978
one more thing, change your code behind to this one:
using System;  
using System.Data;  
using System.Configuration;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Data.SqlClient;  
  
public partial class _Default : System.Web.UI.Page   
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
       
  if (!isPostBack){
        SqlCommand cmd = new SqlCommand("SELECT Customers.CustomerID, COUNT(Orders.OrderID) AS numOrders FROM Customers INNER JOIN Orders ON Customers.CustomerID  = Orders.CustomerID GROUP BY Customers.CustomerID", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));  
  
     try  
     {  
  
      cmd.Connection.Open();  
  
      gvwExample.DataSource = cmd.ExecuteReader();  
  
      gvwExample.DataBind();  
  
      cmd.Connection.Close();  
      cmd.Connection.Dispose();  
  
     }  
  
     catch (Exception ex)  
     {  
      lblStatus.Text = ex.Message;  
     }  
  
  
    }  
}
}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now