Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert c# to asp.net

Posted on 2011-03-03
5
Medium Priority
?
1,204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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:…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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