Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Example of how to do a SQL Server YTD Summary from a Table

Hello Gurus,

This is urgent.

I need a (Best Practice) example of a function on how to do a YTD Summary from a SQL Server Table from an Asp.net web form using VB.  I will then need to place them into 3 variables: YtdHrs, YtdQty and Ytd(OnOrder)
The query I would use would be:

SELECT Sum(Hrs), Sum(Qty), Sum(OnOrder) From Orders Where EnterDate > #12/31/2003#

(I'm not sure if my date syntax is correct. Please correct me if its wrong)

Of particular importance to me is how to extract the information from the returned dataset and placing them into the variables.

Thank you!

Ted
0
tcalbaz
Asked:
tcalbaz
  • 6
  • 2
1 Solution
 
AerosSagaCommented:
Try this: SELECT Sum(Hrs) AS Hours, Sum(Qty) AS Quantity, Sum(OnOrder) As OnOrder From Orders Where EnterDate > '12/31/2003'

Then create your dataset from it and bind it to a datagrid, etc.  you will be able to reference the values using the standard datagrid terminologyr:
eg....
dg.items("Hours")
dg.items("Quantity")
dg.items("OnOrder")

Regards,

Aeros
0
 
nauman_ahmedCommented:
'Set the value of  and SQLConnection SQLDataAdapter
strSQL = "SELECT Sum(Hrs) AS Hours, Sum(Qty) Quantity, Sum(OnOrder) OnOrder From Orders Where EnterDate > #12/31/2003#"
MySQLDataAdapter.Fill(strSQL,"YTD_SUMMARY")

if (MySQLDataAdapter.Tables["YTD_SUMMARY"].Rows.Count >  0) then
   Dim dt As DataTable = MySQLDataAdapter.Tables["YTD_SUMMARY"]
   Dim dr As DataRow = dt.Rows[0]
   Response.Write("Hours: " & dr["Hours"])
   Response.Write("Quantity: " & dr["Quantity"])
   Response.Write("OnOrder: " & dr["OnOrder"])
end if

HTH, :)

Best, nauman
0
 
tcalbazAuthor Commented:
Nauman,
I think your example comes close to what I need to do.  Below is my the actual function I am constructing ased on your idea.  But it is stopping at my Adapter Reference.  Any Suggestions?
Thank You

Ted

-----------------------------------------------------------
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        'Set the value of  and SQLConnection SQLDataAdapter
        Dim MyDataSet As DataSet
        MyDataSet = New DataSet("aNewDataSet")

        Dim MyAdapter As SqlClient.SqlDataAdapter
        Dim MyConnect As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("SQLDB_Test"))
        Dim StrSQL As String
        StrSQL = "SELECT Sum(Hrs) AS Hours, Sum(LT) AS LostTime, Sum(DL) AS DaysLost From TBLEHS620 Where EnterDate > #12/31/2003#"

        MyAdapter.Fill(MyDataSet)   <<<<<<<<<<<<<ERROR: Object reference not set to an instance of an object.
        Dim dt As DataTable = MyDataSet.Tables(0)
        Dim dr As DataRow = dt.Rows(0)

        If (MyDataSet.Tables(0).Rows.Count > 0) Then
            Me.txtHrs.Text = dr("Hours")
            Me.txtLostTime.Text = dr("LostTime")
            Me.txtDaysLost.Text = dr("DaysLost")
        Else
            Me.txtHrs.Text = 0
            Me.txtLostTime.Text = 0
            Me.txtDaysLost.Text = 0
        End If

    End Sub
-----------------------------------------------------------
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nauman_ahmedCommented:
Change  Dim MyDataSet As New DataSet

-Nauman
0
 
nauman_ahmedCommented:
oops sorry, I was wrong. I have the following in C#. Let me do it in VB for more clarity:

private SqlConnection sqlConnection;
private SqlDataAdapter sqlDataAdapter;
private SqlCommand sqlCommand;
sqlConnection.Open();
DataSet ds = new DataSet();
sqlCommand = new SqlCommand();
sqlCommand.CommandText = SQLCommand;
sqlDataAdapter = new SqlDataAdapter(SQLCommand,sqlConnection);
sqlDataAdapter.Fill(ds,TableName);

0
 
nauman_ahmedCommented:
'Set the SQLConnection
Dim MYDataAdapter As SQLDataAdapter
Dim MyDataSet As New DataSet
Dim MySQLCommand As new SQLCommand
MySQLCommand.CommandText = "SELECT Sum(Hrs) AS Hours, Sum(LT) AS LostTime, Sum(DL) AS DaysLost From TBLEHS620 Where EnterDate > #12/31/2003#"

MyDataAdapter = new SQLDataAdapter(SQLComamnd,MySQLConnection)
MyDataAdapter.Fill(MyDataSet,"YTD_SUMMARY")

Please verify if this is working.....

Best, Nauman
0
 
nauman_ahmedCommented:
Also to not confuse the readers: the C# code I have is written as the following function that returns a dataset. tcalbaz you can use the same approach so that you dont have to define the data access layer again and again. Actually I have defined a whole separate class for just Data Access. I call it DatabaseManagement and whenever I need to get the db data, I call it in the following way:

DataSet ds = new DataSet();
DatabaseManagment dbManagement = new DatabaseManagement();
string strSQL = "SELECT * FROM CUSTOMERS";
ds = dbManagement.GetRecords(strSQL,"CUSTOMERS");
DataTable dt = ds.Tables["CUSTOMERS"];
.
.
.
.
//or for a more quick approach....
DataTable dt = dbManagement.GetRecords(strSQL,"CUSTOMERS").Tables["CUSTOMERS"];


This is the function I have defined in the class:

public DataSet GetRecords(string SQLCommand,string TableName)
            {
                  try
                  {
                        sqlConnection.Open();
                        DataSet ds = new DataSet();
                        sqlCommand = new SqlCommand();
                        sqlCommand.CommandText = SQLCommand;
                        sqlDataAdapter = new SqlDataAdapter(SQLCommand,sqlConnection);
                        sqlDataAdapter.Fill(ds,TableName);
                        
                        return ds;
                  
                  }
                  catch(Exception ex)
                  {
                        throw ex;
                  }
                  finally
                  {
                        sqlConnection.Close();

                  }
                  
            }

Make your life easier dude.....;)

-Nauman
0
 
tcalbazAuthor Commented:
Nauman,
Thanks you for your assistance getting me on the right track.
Here is the final function completed:

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim Lastyr As String

        'Year to Date Summary Function
        Dim myQuery As String
        Lastyr = CStr(Now.Year - 1)
        myQuery = "SELECT Sum(Hrs) AS Hours, Sum(LostTime) AS LostTime, Sum(DaysLost) AS DaysLost From TBLEHS620_2 "
        myQuery = myQuery & " WHERE (DateEntered > CONVERT(DATETIME, '" & Lastyr & "-12-31 23:59:59', 102))"

        Dim conn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("SQLDB_Test"))
        conn.Open()
        Dim ds As New DataSet
        Dim adapter As New SqlClient.SqlDataAdapter
        Dim MyCmd As SqlClient.SqlCommand
        adapter.SelectCommand = New SqlClient.SqlCommand(myQuery, conn)
        adapter.Fill(ds)
        Dim dt As DataTable = ds.Tables(0)
        Dim dr As DataRow = dt.Rows(0)
        If (ds.Tables(0).Rows.Count > 0) Then
            Me.txtHrs.Text = dr("Hours")
            Me.txtLostTime.Text = dr("LostTime")
            Me.txtDaysLost.Text = dr("DaysLost")
        Else
            Me.txtHrs.Text = 0
            Me.txtLostTime.Text = 0
            Me.txtDaysLost.Text = 0
        End If

    End Sub
---------------------------------------------
All the best

Ted
0
 
nauman_ahmedCommented:
Thanks ted :) I am glad that my solution worked for you....;) But remember .net is all about object oriented....Always think of reusing the code.....

Cheers, Nauman
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now