Solved

Convert c# to asp.net

Posted on 2011-03-03
5
1,190 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# guarantee sql connection close 6 39
Visual Studio 2013 and Eclipse Neon 6 33
List<PaisEntity> - show some 1 22
Please explain the parts of these 2 LINQ expressions 3 26
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 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

8 Experts available now in Live!

Get 1:1 Help Now