Hi,
I'm new in .net and I want to use a gridview with a datasource in code behind.
This is an example that I copied from the book, but I've got error.
Maybe I need to add some code to web config alså, I'm not sure!
Please help me.
I'm new in .net and I want to use a gridview with a datasource in code behind.
This is an example that I copied from the book, but I've got error.
Maybe I need to add some code to web config alså, I'm not sure!
Please help me.
Code:
<%@ 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 runat="server"> <title>Total Salg</title> </head> <body> <form id="form1" runat="server"> <div> <table cellpadding="10" style="background-color: graytext"> <tr> <td> <b> Daglig Salg</b> <br /> <br /> </td> <td> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Width="179px"> <Columns> <asp:BoundField DataField="SUM(O.QUANTITY)" HeaderText="Antall" ReadOnly="True" SortExpression="SUM(O.QUANTITY)" /> <asp:BoundField DataField="SUM(O.QUANTITY*S.PRICE)" HeaderText="pris" SortExpression="SUM(O.QUANTITY*S.PRICE)" /> </Columns> </asp:GridView> </td> </tr> </table> </div> </form> </body> </html>
Code:
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 { void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } public void BindData() { SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter ad = new SqlDataAdapter("SELECT SUM(o.Quantity), SUM(o.Quantity * s.Price) FROM shamrock.cat_order o, shamrock.cat_member m, shamrock.cat_salesproduct s WHERE ((o.memberid = m.memberid) AND (m.membernum NOT LIKE '214471781')) AND (o.SalesProductID = s.SalesProductNum) AND o.regdate LIKE '05%' AND o.regdate LIKE '05/10/2007%' ", myConnection); DataSet ds = new DataSet(); ad.Fill(ds, "Categories"); GridView1.DataSource = ds; GridView1.DataBind(); } }
Comment