Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 11th, 2007, 07:54 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default DateTime questions And SQL Data Objects ?

a) I have to admit, I am very knew at creating SQL data objects. I get a problem when trying to page with my data source. I know it has to do with my use of a Data Reader. I am pretty sure I need to use a DataSet--but I am not sure how to rewrite the following code to do that---could someone help?

Dim knightsDBConn As New SqlConnection(conString)
        Dim sqlString As String = "GetEventsByDate"
        Dim sqlCmd As New SqlCommand(sqlString, knightsDBConn)
        sqlCmd.CommandType = CommandType.StoredProcedure
        sqlCmd.Parameters.AddWithValue("@EventFromDate", fromDate)
        sqlCmd.Parameters.AddWithValue("@EventToDate", toDate)
        knightsDBConn.Open()
        Dim diaryEventSQLDR As SqlDataReader = sqlCmd.ExecuteReader(CommandBehavior.CloseConnecti on)
        sqlCmd = Nothing
        Return diaryEventSQLDR

b) second question is--does anyone know how to put code as a default value of a Gridview. I am trying to do something like "DefaultValue=datetime.now" && "DefaultValue=datetime.now.addmonths(1)"
Basically, I have a GridView that is using an ObjectDataSource that grabs parameters from two text boxes I have....and I am wanting to set the defaultvalue to above--not sure how to do that without the control converting to string.

THANKS SO MUCH!
Rob
 
Old January 12th, 2007, 04:51 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rob,

There are a couple of ways to do this. Probably the easiest way is to use the Load method on the DataSet or DataTable class:
Code:
private DataTable GetDataTable()
{
  string sql = "YourSelectStatement";
  using (SqlConnection myConnection = new SqlConnection(connectionString))
  {
    using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
    {
      myConnection.Open();
      using (SqlDataReader myReader = myCommand.ExecuteReader())
      {
        DataTable myTable = new DataTable();
        myTable.Load(myReader);
        myConnection.Close();
        return myTable;
      }
    }
  }
}
(Taken from http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=406 and therefore in C#, but I think you get the idea)Alternatively, you can use the "old" way and use an Adapter to fill the DataSet:
Code:
Using myConnection As SqlConnection = New SqlConnection(connectionString)
  Dim myAdapter As SqlDataAdapter = New SqlDataAdapter()
  myConnection.Open()

  Dim myCommand As SqlCommand = New SqlCommand(yourSqlStatement, myConnection)
  myCommand.CommandType = CommandType.Text ' Or StoredProcedure for a procedure

  myAdapter.SelectCommand = myCommand

  Dim myDataSet As DataSet = New DataSet("TableName")
  myAdapter.Fill(myDataSet)

  myConnection.Close()
End Using
Finally, to set a parameters value, use the OnSelecting event. First, let's assume you have a single parameter in the <SelectParameters>:
Code:
<SelectParameters>
  <asp:Parameter Name="today" Type="Object" />
</SelectParameters>
Then you can handle the OnSelecting event like this:
Code:
  Protected Sub ObjectDataSource1_Selecting( _
        ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) _
        Handles ObjectDataSource1.Selecting
    e.InputParameters(0) = DateTime.Now
  End Sub
  By the way, if you just want to pass today's date, I think you're better using that date in the object directly.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
sql datetime type angelboy C# 2005 0 April 16th, 2007 03:02 PM
paging through data - Data Access Objects fedoracore PHP Databases 0 November 28th, 2006 07:54 PM
selecting data in datetime intervals Phathu SQL Server 2005 1 August 29th, 2006 03:53 PM
Failed to copy objects from SQL server to SQL Serv monfu SQL Server 2000 4 December 4th, 2005 05:54 PM
SQL datetime problem xpphantom SQL Server 2000 5 September 30th, 2004 08:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.