[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1206
  • Last Modified:

Convert c# to asp.net

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
hiddenjob
Asked:
hiddenjob
  • 2
  • 2
1 Solution
 
DhaestCommented:
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
 
hiddenjobAuthor Commented:
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
 
hiddenjobAuthor Commented:
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
 
DhaestCommented:
If you debug it, do you get any error ?
0
 
PagodNaUtakCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now