count the element in a database tablehttp://forums.asp.net/t/298536.aspx/1?count+the+element+in+a+database+tableMon, 04 Aug 2003 09:01:57 -0400298536298536http://forums.asp.net/p/298536/298536.aspx/1?count+the+element+in+a+database+tablecount the element in a database table i have a problem here, where i would like to count how many elements on the table. i am using ms access as my database and OLEDB. Let's say, the purchase_table, i would like to know how many item that a particular customer purchase. can anyone pls help me on this? 2003-08-03T12:20:55-04:00298623http://forums.asp.net/p/298536/298623.aspx/1?Re+count+the+element+in+a+database+tableRe: count the element in a database table Assuming that you have a field called CustomerID that identifies the purchaser then something like this should work: <pre class="prettyprint">int customerIDValue = theCustomerIDYouWantToUse; OleDbCommand oledb = new OleDbCommand(); OleDbConnection connection = new OleDbConnection( &quot;YourConnectionString&quot; ); oledb.CommandType = CommandType.TableDirect; oledb.CommandText = &quot;SELECT Count(*) AS CustomerPurchaseCount FROM purchase_table Where CustomerID=@CustomerID&quot;; OleDbParameter sp1 = oledb.Parameters.Add( new OleDbParameter(&quot;@CustomerID&quot;,OleDbType.Integer,4 )); sp1.Value = customerIDValue; connection.Open(); int count = (int)oledb.ExecuteScalar(); connection.Close();</pre> 2003-08-03T16:50:56-04:00298842http://forums.asp.net/p/298536/298842.aspx/1?Re+count+the+element+in+a+database+tableRe: count the element in a database table thanks a lot for your help. This is the code that i have tried, but it doesn't work....when i tried to use commandtype.Tabledirect, the error message say &quot;Object reference not set to an instance of an object.&quot; Public Function GetItemCount(ByVal cartID As String) As Integer ' Create Instance of Connection and Command Object Dim myConnection2 As OleDbConnection Dim myCommand As OleDbCommand Dim qry As String Dim NumRec As Integer myConnection2 = New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source= E:\webpage\Database\test.mdb&quot;) qry = &quot;SELECT COUNT(*) FROM shoppingCart WHERE CartID = @CartID&quot; myCommand = New OleDbCommand(qry, myConnection2) 'i have tried this code as well 'myCommand.CommandType = CommandType.TableDirect 'myCommand.CommandText = &quot;SELECT COUNT(*) As NumRec FROM shoppingCart WHERE CartID = @CartID&quot; 'Dim parameterCartID As OleDbParameter = New OleDbParameter(&quot;@CartID&quot;, OleDbType.VarChar, 10) 'parameterCartID.Value = cartID 'myCommand.Parameters.Add(parameterCartID) ' Open the connection and execute the Command myConnection2.Open() myCommand.ExecuteScalar() myConnection2.Close() Return CInt(NumRec) End Function //end of code what's wrong with the code???? thanks christene 2003-08-04T02:44:03-04:00298900http://forums.asp.net/p/298536/298900.aspx/1?Re+count+the+element+in+a+database+tableRe: count the element in a database table I'm not sure but I think because you used the constructor for OleDbCommand that takes the query as an arguement that you then can't set CommandType. Maybe try this: <pre class="prettyprint">qry = &quot;SELECT COUNT(*) FROM shoppingCart WHERE CartID = @CartID&quot; myCommand = New OleDbCommand() myCommand.Connection = myConnection2 'myCommand.CommandType = CommandType.TableDirect myCommand.CommandText = qry</pre> 2003-08-04T05:05:46-04:00298966http://forums.asp.net/p/298536/298966.aspx/1?Re+count+the+element+in+a+database+tableRe: count the element in a database table well,i have tried the code....and this is the code that i run, Public Function GetItemCount(ByVal cartID As String) As Integer ' Create Instance of Connection and Command Object Dim myConnection2 As OleDbConnection Dim myCommand As OleDbCommand Dim qry As String Dim NumRec As Integer myConnection2 = New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source= E:\webpage\Database\test.mdb&quot;) qry = &quot;SELECT COUNT(*) As NumRec FROM shoppingCart WHERE CartID = @CartID&quot; myCommand = New OleDbCommand myCommand.Connection = myConnection2 myCommand.CommandType = CommandType.TableDirect myCommand.CommandText = qry Dim parameterCartID As OleDbParameter = New OleDbParameter(&quot;@CartID&quot;, OleDbType.VarChar, 10) parameterCartID.Value = cartID myCommand.Parameters.Add(parameterCartID) ' Open the connection and execute the Command myConnection2.Open() myCommand.ExecuteScalar() myConnection2.Close() Return CInt(NumRec) End Function //end of code However, it says &quot;too few parameter.Expected 1.&quot; for the myCommand.ExecuteScalar(). what is that mean?how shold i solve this? thanks. christene 2003-08-04T07:52:43-04:00298992http://forums.asp.net/p/298536/298992.aspx/1?Re+count+the+element+in+a+database+tableRe: count the element in a database table well well...i have done the COUNT() with this code.. Public Function GetItemCount(ByVal cartID As String) As Integer ' Create Instance of Connection and Command Object Dim myConnection2 As OleDbConnection Dim myCommand As OleDbCommand Dim qry As String Dim NumRec As Integer myConnection2 = New OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source= E:\webpage\Database\test.mdb&quot;) qry = &quot;SELECT COUNT(*) FROM shoppingCart Where CartID= '&quot; &amp; cartID &amp; &quot;'&quot; myCommand = New OleDbCommand(qry, myConnection2) Dim parameterCartID As OleDbParameter = New OleDbParameter(&quot;@CartID&quot;, OleDbType.VarChar, 10) parameterCartID.Value = cartID myCommand.Parameters.Add(parameterCartID) ' Open the connection and execute the Command myCommand.Connection.Open() NumRec = myCommand.ExecuteScalar() myConnection2.Close() Return CInt(NumRec) End Function Thanks a lot for guiding me!!!.... christene 2003-08-04T09:01:57-04:00