Navigation

Search

Categories

On this page

Showing Database User Connections
Using Different Parameter Types with the ObjectDataSource control
Using Parameters with the ObjectDataSource control

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 378
This Year: 6
This Month: 1
This Week: 0
Comments: 17

Sign In
Pick a theme:

# Wednesday, December 29, 2010
Wednesday, December 29, 2010 6:08:03 PM (GMT Standard Time, UTC+00:00) ( ObjectDataSource Control | SQL )
How to show the some connection stats on your SQL Server database
<script runat="server">

Private Sub Page_Load()
Dim connectionString As String = "Min Pool Size=10;data source=x.x.x.x;initial catalog=Bonds;user id=user;password=mypassword"
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand("SELECT * FROM master..sysprocesses WHERE hostname<>''", con)
Using con


con.Open()
grdStats.DataSource = cmd.ExecuteReader()
grdStats.DataBind()

End Using
End Sub
</script>

<asp:GridView
id="grdStats"
Runat="server" />
Comments [0] | | # 
# Tuesday, April 15, 2008
Tuesday, April 15, 2008 8:03:57 PM (GMT Daylight Time, UTC+01:00) ( ASP.NET | ObjectDataSource Control )

You can use all of the same types of parameters with the ObjectDataSource control that you can use with the SqlDataSource

control.  See this page for more info and a demo.

Comments [0] | | # 
Tuesday, April 15, 2008 8:00:01 PM (GMT Daylight Time, UTC+01:00) ( ASP.NET | ObjectDataSource Control )

In this example notice that the ObjectDataSource control includes an UpdateMethod property that points to the UpdateMovie() method. See a working demo of this.

The GridView automatically adds the update parameters to the ObjectDataSource control's UpdateParameter collection. As an alternative, you can declare the parameters used by the ObjectDataSource control explicitly.  See this example for details on this.

 

*** Movies.vb ***

Imports System.Web.Configuration

Public Class Movies

    Private ReadOnly _conString As String

    Public Sub UpdateMovie(ByVal id As Integer, ByVal title As String, &_ 
    ByVal director As String, ByVal dateReleased As DateTime)
        ' Create Command
        Dim con As New SqlConnection(_conString)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "UPDATE Movies SET Title=@Title,Director=@Director,&_ 
        DateReleased=@DateReleased WHERE Id=@Id"

        ' Add parameters
        cmd.Parameters.AddWithValue("@Title", title)
        cmd.Parameters.AddWithValue("@Director", director)
        cmd.Parameters.AddWithValue("@DateReleased", dateReleased)
        cmd.Parameters.AddWithValue("@Id", id)

        ' Execute command
        Using con
            con.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Sub

    Public Function GetMovies() As SqlDataReader
        ' Create Connection
        Dim con As New SqlConnection(_conString)

        ' Create Command
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "SELECT Id,Title,Director,DateReleased FROM Movies"

        ' Return DataReader
        con.Open()
        Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
    End Function

    Public Sub New()
        _conString = WebConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString
    End Sub

End Class


*** ShowMovies.aspx ***

<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Movies</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:GridView
        id="grdMovies"
        DataSourceID="srcMovies"
        DataKeyNames="Id"
        AutoGenerateEditButton="true"
        Runat="server" />
        
    <asp:ObjectDataSource
        id="srcMovies"
        TypeName="Movies"
        SelectMethod="GetMovies"
        UpdateMethod="UpdateMovie"
        Runat="server"/>
    
    </div>
    </form>
</body>
</html>
Comments [0] | | #