|
.. this is continued from my other post. I'm looking for some help in calculating totals for a shopping cart datagrid. So I'll post the code I'm using to generate this datagrid. I'm actually not using a stored procedure. I just wrote an inner-join select statement which I put in a dataset to bind to the datagrid.
'making shopping cart grid
Dim Ordernumber As Integer = CType(Session.Item("ordernum"), Integer)
' old select string Dim strSelect1 As String = "SELECT * from pOrderItembl where po_nbr = '" & Ordernumber & "'"
Dim strSelect1 As String = "select ppartbl.part_id, ppartbl.part_name, ppartbl.part_price, porderitembl.item_qty, porderitembl.po_nbr from ppartbl inner join porderitembl on ppartbl.part_id = porderitembl.part_id where po_nbr = '" & Ordernumber & "'"
Dim objDataSet1 As New DataSet
Try
Dim objDataAdapter As New SqlDataAdapter(strSelect1, dbConn)
objDataAdapter.Fill(objDataSet1, "pOrderItembl")
Catch objError As Exception
dbprob.Text = "<b>* Error in updating the database</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub
End Try
Dim objdataview1 As New DataView(objDataSet1.Tables("porderitembl"))
DataGrid1.DataSource = objdataview1
DataGrid1.DataBind()
<Added>
Also, in terms of those two options you gave me. It doesn't really matter to me whether I calculate the total inside or outside the datagrid, as long as I can display it on the same page. Its just the task of running through the datagrid to make all the calculations (item_qty * part_price) that I'm having trouble with.
Thanks
|
|
|
yes that would help me out a great deal, thanks!
|
|
|
Sorry this took so long, I couldn't get to it until today.
I created a very simple database, that only contains a single column of numbers. The connection string is stored in web.config. You get data out of web.config by using the System.Configuration class. So my line of code:
connTGREER.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"]; |
|
is a way to connect to the database without putting your username/password etc. in the code. It's both more secure and more portable to keep your connection strings in web.config.
Here's the fully commented code demonstrating the technique I prefer to use:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
// the above added by VS.NET
// TGREER added:
using System.Data.SqlClient;
using System.Configuration;
namespace dgArray
{
public class WebForm1 : System.Web.UI.Page
{
// DataGrid was dragged and placed on webform:
protected System.Web.UI.WebControls.DataGrid DataGrid1;
// TGREER added:
// DataBase objects:
protected System.Data.SqlClient.SqlConnection connTGREER;
protected System.Data.SqlClient.SqlCommand cmdTGREER;
protected System.Data.SqlClient.SqlDataReader drTGREER;
// ArrayList to contain database data, and integer for Total:
// Note: You may need a different datatype, integer's don't work
// for money:
public ArrayList myArray;
public int myTotal = 0;
private void Page_Load(object sender, System.EventArgs e)
{
// TGREER - Create Database objects:
connTGREER = new SqlConnection();
connTGREER.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
connTGREER.Open();
cmdTGREER = new SqlCommand();
cmdTGREER.Connection = connTGREER;
// perform query (NOTE: I recommend using Stored Procedure instead:
cmdTGREER.CommandText = "select qty from dgArray";
drTGREER = cmdTGREER.ExecuteReader();
// create the array:
myArray = new ArrayList();
// read the DataReader
while(drTGREER.Read())
{
// adding the data from database to the array:
myArray.Add(drTGREER["qty"].ToString());
}
// Now, loop through array, calculating total:
for (int i=0; i< myArray.Count; i++)
{
myTotal = myTotal + System.Convert.ToInt32(myArray[ i ]);
}
// Add the total to the array:
myArray.Add(myTotal);
// Bind the array to the DataGrid:
DataGrid1.DataSource = myArray;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
|
|
Note, the line:
myTotal = myTotal + System.Convert.ToInt32(myArray[ i ]); |
|
I had to pad the " [ i ] " with spaces so the forum wouldn't interpret it as an "italic" tag. You will take the spaces out. The forum really, really needs a "code" tag so that code postings will maintain their formatting.
As always, both my site and CodeToad are advertiser supported. Show your appreciation by visiting sponsors.
<Added>
A link to this application:
http://www.tgreer.net/dgArray/WebForm1.aspx
|
|
|
|
|
thanks for all your help, I appreciate it.
|
|
|
|
|
|
|
|