Solved

Convert c# to asp.net

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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