Navigation

Search

Categories

On this page

Checkbox Control – Populating From Database
SQL Search – RedGate Software
How to pass more than one parameter in a HyperLinkField

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: 245
This Year: 51
This Month: 0
This Week: 0
Comments: 0

Sign In
Pick a theme:

# Friday, July 16, 2010
Friday, July 16, 2010 8:37:11 PM (GMT Daylight Time, UTC+01:00) ( ASP.NET )


This demonstration will populate a ASP.NET checkbox control, check the matching records saved in a database and then save any changes made by the user
back to the database

'This first populates a checkboxlist control with a list of records 
'then loops back through the control and checks the checkboxes which have been saved to the database 

Public Sub BindAlertsList() 

        'This creates the list of checkboxes for the alert events types 
        Dim strSQLConn As SqlConnection
        Dim cmd As SqlCommand
        Dim objReader As SqlDataReader
        Dim list_id As Integer = CInt(lbllist_id.Text)
        strSQLConn = New SqlConnection(ConfigurationManager.ConnectionStrings("Tearsheet_ConString").ConnectionString)
        cmd = New SqlCommand("SELECT ' ' + [AlertEvent] AS AlertEvent, [alert_id] FROM SavedCompanyListAlertValues ORDER BY AlertEvent", strSQLConn) 

        strSQLConn.Open()
        cblAlertTypes.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        cblAlertTypes.DataTextField = "AlertEvent"
        cblAlertTypes.DataValueField = "alert_id"
        cblAlertTypes.DataBind()
        'Dim cmd As SqlCommmand
        cmd = New SqlCommand("SELECT * FROM SavedCompanyListAlertTypes WHERE list_id=" & list_id, strSQLConn)
        strSQLConn.Open()
        objReader = cmd.ExecuteReader()
        While objReader.Read()
            Dim currentCheckBox As ListItem = cblAlertTypes.Items.FindByValue(objReader("alert_id").ToString())
            If Not (currentCheckBox Is Nothing) Then
                currentCheckBox.Selected = True
            End If
        End While
        strSQLConn.Close()
End Sub 

'This saves the checked records in the database 

Sub SaveAlerts_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'The first step in the save alerts process is to delete all of the exising ones for the selected company list
        DeleteAlertTypes()
End Sub
    Sub DeleteAlertTypes()
        'The first step in the save alerts process is to delete all of the exising ones for the selected company list
        Dim strSQLConn As SqlConnection
        Dim cmd As SqlCommand
        Dim SqlText As String
        Dim intlist_id As Integer = CInt(lbllist_id.Text)
        strSQLConn = New SqlConnection(ConfigurationManager.ConnectionStrings("Tearsheet_ConString").ConnectionString)
        SqlText = "p_SavedCompanyList_Delete_AlertTypes"
        cmd = New SqlCommand(SqlText)
        cmd.Connection = strSQLConn
        cmd.CommandType = CommandType.StoredProcedure
        'input parameters for the sproc
        cmd.Parameters.Add("@list_id", SqlDbType.Int).Value = intlist_id
        Try
            strSQLConn.Open()
            cmd.Connection = strSQLConn
            cmd.ExecuteNonQuery()
        Finally
            strSQLConn.Close()
        End Try
        'Now that the existing alert types are deleted, we can add the new ones
        SaveAlertTypes()
    End Sub 

Sub SaveAlertTypes()
        Dim strSQLConn As SqlConnection
        Dim cmd As SqlCommand
        Dim SqlText As String
        Dim intlist_id As Integer = CInt(lbllist_id.Text)
        'Make the lblError label invisible in case the user is resubmitting
        lblError.Visible = False 

        strSQLConn = New SqlConnection(ConfigurationManager.ConnectionStrings("Tearsheet_ConString").ConnectionString)
        SqlText = "p_SavedCompanyList_Save_AlertTypes"
        Try
            strSQLConn.Open()
            For Each Item As ListItem In cblAlertTypes.Items
                If (Item.Selected) Then
                    cmd = New SqlCommand(SqlText)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Connection = strSQLConn
                    'input parameters for the sproc
                    cmd.Parameters.Add("@list_id", SqlDbType.Int).Value = intlist_id
                    cmd.Parameters.Add("@alertevents", SqlDbType.Int).Value = Item.Value
                    cmd.ExecuteNonQuery()
                End If
            Next
        Finally
            strSQLConn.Close()
            lblSaveAlerts.Visible = True
        End Try
End Sub 

<asp:CheckboxList ID="cblAlertTypes" runat="server" 
CssClass="smalltextNoUnderline" BorderStyle="Solid" BorderWidth="2px">                               
</asp:CheckboxList> 

CREATE PROCEDURE [dbo].[p_SavedCompanyList_Delete_AlertTypes]
    @list_id int    
AS
BEGIN 

    SET NOCOUNT ON; 

    DELETE FROM 
        SavedCompanyListAlertTypes
    WHERE 
        list_id = @list_id
END 

CREATE PROCEDURE p_SavedCompanyList_Save_AlertTypes
    @list_id int,
    @alertevents int
AS
BEGIN 

INSERT INTO SavedCompanyListAlertTypes
    (list_id, alertevents)
VALUES
    (@list_id, @alertevents) 

END
GO    
Comments [0] | | # 
# Wednesday, July 14, 2010
Wednesday, July 14, 2010 1:50:43 PM (GMT Daylight Time, UTC+01:00) ( SQL )


A very nice little free tool from RedGate software which gives you the ability to search through all of your SQL objects.  Examples from RedGate on how it is useful include:

    You want to rename one of your table columns but aren't sure what stored procedures reference it. Using SQL Search, you can search for the column name and find all the stored procedures where it is used.

    Finding anything in the SSMS object tree requires a lot of clicking. Using SQL Search, you can press the shortcut combo, start typing the name, and jump right there.

    You need to find stored procedures you’ve not yet finished writing. Using SQL Search, you can search for stored procedures containing the text 'TODO'.

    You are a DBA, and developers keep using 'SELECT *' in their views and stored procedures. You want to find all these and replace them with a correct list of columns to improve performance and prevent future bugs. Using SQL Search, you can look for 'SELECT *' in the text of stored procedures and views.

You can download the free tool here.

Comments [0] | | # 
# Wednesday, July 07, 2010
Wednesday, July 07, 2010 9:34:17 PM (GMT Daylight Time, UTC+01:00) ( Gridview )

<
asp:HyperLinkField HeaderText="" Text="View" DataNavigateUrlFields="list_id,ListName" DataNavigateUrlFormatString="list.view.aspx?list_id={0}&ListName={1}"> </asp:HyperLinkField>
Comments [0] | | #