aic007 Posted April 25, 2007 Share Posted April 25, 2007 Hi, could someone please help me to tranelate this code in to c#. ... I am very new to ASP.net and have only programmed in c#, don't understand VB...<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><%@ register tagprefix="web" namespace="WebChart" assembly="WebChart"%><script runat="server" language="VB"> Sub Page_Load(sender as Object, e as EventArgs) If Not Page.IsPostBack then 'STEP 1: Get the data from the database Dim myConnection as New SqlConnection(Connection String) Const strSQL as String = "SELECT TOP 6 Name, ViewCount " & _ "FROM tblFAQCategory ORDER BY NEWID()" Dim myCommand as New SqlCommand(strSQL, myConnection) Dim reader as SqlDataReader = myCommand.ExecuteReader() 'STEP 2: Create the chart object Dim chart as New PieChart 'STEP 3: Bind the DataTable to the WebChart chart.DataSource = reader chart.DataXValueField = "Name" chart.DataYValueField = "ViewCount" chart.DataBind() chart.DataLabels.Visible = True 'STEP 4: Attach the chart object to the chart container ChartControl1.Charts.Add(chart) ChartControl1.RedrawChart() reader.Close() myConnection.Close() End If End Sub</script><web:chartcontrol runat="server" id="ChartControl1" height="400" width="350" gridlines="none" legend-position="Bottom" / Link to comment Share on other sites More sharing options...
pulpfiction Posted April 25, 2007 Share Posted April 25, 2007 Im new to C#, just giving it a try... correct me if wrong. private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { //STEP 1: Get the data from the database SqlConnection myConnection = new SqlConnection("Connection String"); const string strSQL = @"SELECT TOP 6 Name, ViewCount " + "FROM tblFAQCategory ORDER BY NEWID()"; SqlCommand myCommand = new SqlCommand(strSQL, myConnection); SqlDataReader reader = new SqlDataReader(); reader = myCommand.ExecuteReader(); //STEP 2: Create the chart object PieChart chart = new PieChart(); //STEP 3: Bind the DataTable to the WebChart chart.DataSource = reader; chart.DataXValueField = "Name"; chart.DataYValueField = "ViewCount"; chart.DataBind(); chart.DataLabels.Visible = true; //STEP 4: Attach the chart object to the chart container ChartControl1.Charts.Add(chart); ChartControl1.RedrawChart(); reader.Close(); } } Link to comment Share on other sites More sharing options...
aic007 Posted April 25, 2007 Author Share Posted April 25, 2007 Yeay, thanx a lot man. Link to comment Share on other sites More sharing options...
pulpfiction Posted April 25, 2007 Share Posted April 25, 2007 no problem, did you test the code, is it working fine? Link to comment Share on other sites More sharing options...
aic007 Posted April 26, 2007 Author Share Posted April 26, 2007 Hi.I think your code is perfect, but I get some errors, and that's because I am missing some statements at "the top".Can you please help me with this;<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><%@ register tagprefix="web" namespace="WebChart" assembly="WebChart"%><script runat="server" language="VB">Thanks a lot, once again. Link to comment Share on other sites More sharing options...
jesh Posted April 26, 2007 Share Posted April 26, 2007 I think the only change you'll need to make is this: <script runat="server" language="C#">EDIT: Or maybe, this:<script type="text/C#" runat="server"> Link to comment Share on other sites More sharing options...
aspnetguy Posted April 26, 2007 Share Posted April 26, 2007 is text/C# even a valid type? I have only ever seen language="C#".Remember this is for the .net compiler only. It is not rendered with the HTML so their is not need to worry about html validation or mark up rules for it. Link to comment Share on other sites More sharing options...
jesh Posted April 26, 2007 Share Posted April 26, 2007 Intellisense in VS 2005 says that's the way to do it. I never run C# code in the aspx pages though, I always run it in a code behind. I'd probably stick with 'language="C#"' if I put some code on the page. Link to comment Share on other sites More sharing options...
pulpfiction Posted April 26, 2007 Share Posted April 26, 2007 We can also write <% @Page Language="C#" %> at the top of the page and code between <%%> right?? Link to comment Share on other sites More sharing options...
jesh Posted April 26, 2007 Share Posted April 26, 2007 We can also write <% @Page Language="C#" %> at the top of the page and code between <%%> right??Yes, that's right.I typically, however, set up my page declaration like this (in ASP.NET 2.0):<%@ Page Language="c#" Inherits="MyPage" CodeFile="MyPage.aspx.cs" %> And all my code is in that MyPage.aspx.cs (codebehind) file. The syntax differs a bit on .NET 1.1: <%@ Page Language="c#" Inherits="MyPage" Codebehind="MyPage.aspx.cs" %> Link to comment Share on other sites More sharing options...
aspnetguy Posted April 27, 2007 Share Posted April 27, 2007 Intellisense in VS 2005 says that's the way to do it. I never run C# code in the aspx pages though, I always run it in a code behind. I'd probably stick with 'language="C#"' if I put some code on the page.I rarely run inline code either I love the seperation of code behind. Link to comment Share on other sites More sharing options...
pulpfiction Posted April 27, 2007 Share Posted April 27, 2007 I rarely run inline code either I love the seperation of code behind.Thats the advantage of .NET, to seperate presentation from logic.... if we are writing inline it will be more like ASP again... Link to comment Share on other sites More sharing options...
aic007 Posted April 28, 2007 Author Share Posted April 28, 2007 Hi again. I need help with code again. I can not configure what to do in order to get rid of this error message in my .aspx file. This is the error message I get.Error 1 The type 'System.Data.SqlClient.SqlDataReader' has no constructors defined This is my code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Statistics.aspx.cs" Inherits="Statistikk" %><%@ Register TagPrefix="Web" Namespace="WebChart" Assembly="WebChart" %><%@ Import Namespace="System.Drawing" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><%@ register tagprefix="web" namespace="WebChart" assembly="WebChart"%><script runat="server"> private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { //STEP 1: Get the data from the database SqlConnection myConnection = new SqlConnection("Connection String"); const string strSQL = @"SELECT TOP 6 Name, ViewCount " + "FROM tblFAQCategory ORDER BY NEWID()"; SqlCommand myCommand = new SqlCommand(strSQL, myConnection); SqlDataReader reader = new SqlDataReader(); reader = myCommand.ExecuteReader(); //STEP 2: Create the chart object PieChart chart = new PieChart(); //STEP 3: Bind the DataTable to the WebChart chart.DataSource = reader; chart.DataXValueField = "Name"; chart.DataYValueField = "ViewCount"; chart.DataBind(); chart.DataLabels.Visible = true; //STEP 4: Attach the chart object to the chart container ChartControl1.Charts.Add(chart); ChartControl1.RedrawChart(); reader.Close(); } } </script><!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 runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <br /> <Web:ChartControl runat="Server" id="ChartControl1" height="400" Width="350" GridLines="None" Legend-Position="Bottom"/> </div> </form></body></html> Plzz help me Link to comment Share on other sites More sharing options...
aspnetguy Posted April 29, 2007 Share Posted April 29, 2007 change this SqlDataReader reader = new SqlDataReader();reader = myCommand.ExecuteReader(); to SqlDataReader reader = myCommand.ExecuteReader(); some objects can't be set with "new". sometimes you have to use a function (like command.ExecuteReader()) that returns the type it needs. Link to comment Share on other sites More sharing options...
aic007 Posted April 29, 2007 Author Share Posted April 29, 2007 Very nice, thanx alot. One problem fixed and another one arises :)Now I get errormessages on this code;SqlConnection myConnection = new SqlConnection("connectionString");The error message is; "Format of the initialization string does not conform to specification starting at index 0."I think this also has something to do with "new", but how can I avoid using "new SqlConnection" here ? Link to comment Share on other sites More sharing options...
aspnetguy Posted April 30, 2007 Share Posted April 30, 2007 in this case you do need to use new SqlConnection here. Add this to the <configuration> of your web.config file <appSettings> <add key="ConnectionString" value="Data Source=(local);Initial Catalog=databaseName;Trusted_Connection=yes;" /> </appSettings> Then you can use the following whenever you need to initialize your connection using System.Data.SqlClient;using System.Configuration;...SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); Link to comment Share on other sites More sharing options...
aic007 Posted May 2, 2007 Author Share Posted May 2, 2007 I still can not get it to work, but thanx a lot of tryring to help.I will try a new code. Could somebody help me tranelate this to c # <%@ Page Language="VB" %><%@ Register tagPrefix="web" Assembly="WebChart" Namespace="WebChart" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.OleDb" %><script runat=server>Sub Page_Load(o as object, e as EventArgs) Dim reader As IDataReader = GetReader() Dim chart As New SmoothLineChart() chart.DataXValueField = "Product" chart.DataYValueField = "Price" chart.DataSource = reader chart.DataBind() reader.Close() ChartControl1.Charts.Add(chart) ChartControl1.RedrawChart()End SubFunction GetReader() As IDataReader Dim connection As new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB;Mode=Read") Dim command As new OleDbCommand("SELECT Top 10 [ProductName] As Product,[unitPrice] As Price FROM Products order by UnitPrice desc", connection) connection.Open() Return command.ExecuteReader(CommandBehavior.CloseConnection)End Function</script><html><body><form runat="server"> <web:ChartControl runat="server" Width="800px" Height="600px" id="ChartControl1" HasChartLegend="false" BottomChartPadding=80 ChartPadding=20> <XAxisFont ForeColor="Black" StringFormat="Center,Center,Character,DirectionVertical" Font="Tahoma, 8pt, style=bold" /> </web:ChartControl></form></body></html> Link to comment Share on other sites More sharing options...
aspnetguy Posted May 2, 2007 Share Posted May 2, 2007 void Page_Load(object o, EventArgs e){ IDataReader reader = GetReader(); SmoothLineChart chart = new SmoothLineChart(); chart.DataXValueField = "Product"; chart.DataYValueField = "Price"; chart.DataSource = reader; chart.DataBind(); reader.Close(); ChartControl1.Charts.Add(chart); ChartControl1.RedrawChart();}IDataReader GetReader(){ OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Program Files\\Microsoft Visual Studio\\VB98\\NWIND.MDB;Mode=Read"); OleDbCommand command = new OleDbCommand("SELECT Top 10 [ProductName] As Product,[UnitPrice] As Price FROM Products order by UnitPrice desc", connection); connection.Open(); return command.ExecuteReader(CommandBehavior.CloseConnection);} Link to comment Share on other sites More sharing options...
aic007 Posted May 2, 2007 Author Share Posted May 2, 2007 You da man, aspnetguy, thanx a lot. Link to comment Share on other sites More sharing options...
aspnetguy Posted May 3, 2007 Share Posted May 3, 2007 no problem. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.