Wednesday, November 23, 2016

ASP.NET - One Radio Button Selected in Gridview

ASP.NET does not allow you to place a radio button in a gridview row and have only a single radio button selected at a time.

I came across this post that solved by problem.

http://www.dotnetlearners.com/blogs/view/216/allow-only-single-radio-button-selection-in-aspnet-gridview.aspx

It uses this javascript function to change the other selected value:

<script language="javascript">
    function singleRbtnSelect(chb) {
        $(chb).closest("table").find("input:radio").prop("checked", false);
        $(chb).prop("checked", true);
    }
</script>

Tuesday, September 20, 2016

SQL - Query Triggers in a Database

SELECT 
     o.name AS trigger_name 
    ,'x' AS trigger_owner 
    ,s.name AS table_schema 
    ,OBJECT_NAME(o.parent_obj) AS table_name 
    ,OBJECTPROPERTY(o.id, 'ExecIsUpdateTrigger') AS isupdate 
    ,OBJECTPROPERTY(o.id, 'ExecIsDeleteTrigger') AS isdelete 
    ,OBJECTPROPERTY(o.id, 'ExecIsInsertTrigger') AS isinsert 
    ,OBJECTPROPERTY(o.id, 'ExecIsAfterTrigger') AS isafter 
    ,OBJECTPROPERTY(o.id, 'ExecIsInsteadOfTrigger') AS isinsteadof 
    ,OBJECTPROPERTY(o.id, 'ExecIsTriggerDisabled') AS [disabled] 
FROM sysobjects AS o 

INNER JOIN sysobjects AS o2 
    ON o.parent_obj = o2.id 

INNER JOIN sysusers AS s 
    ON o2.uid = s.uid 

WHERE o.type = 'TR'

*Credit goes to the poster here: Stack Overflow - Get Triggers in Database

Thursday, March 24, 2016

VB.NET - Execute Command Lines

To call a command prompt in one line, it can be done this way:

Process.Start("cmd", "/c cd " & m_strSignToolLocation & " & " & strSignCmd)

To call a command prompt with more options, such as running with the command prompt hidden, call this way:

Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = "/c cd " & m_strSignToolLocation & " & " & strSignCmd
pi.FileName = "cmd.exe"
pi.WindowStyle = ProcessWindowStyle.Hidden

p.StartInfo = pi

p.Start()

Here are what some of the command arguments mean:

'/k returns the command window
'/c terminates the command window

'& allows multi line execution on a single line

Thursday, February 11, 2016

SQL - Find Stored Procedures that have been Renamed

The following script will determine if any stored procedures were renamed, rather than dropped and re-created (substitute [DATABASE NAME] with the name of your database):


     SELECT meta.ROUTINE_NAME, *
     FROM [DATABASE NAME].sys.sql_modules def,
           [DATABASE NAME].INFORMATION_SCHEMA.ROUTINES meta
     WHERE  SUBSTRING(def.definition,1,4000) = SUBSTRING(meta.ROUTINE_DEFINITION,1,4000)
AND def.definition NOT LIKE '%![' + meta.ROUTINE_NAME + '!]%' ESCAPE '!'   
order by 1



Thursday, December 10, 2015

VB.NET - Search Datagridview and Select Cell

The following function will search for a string in a datagridview and then select the cell with the found string. Continuing to call this function on a button click will cycle through the search moving to the next found string until the end of the grid is reached.


    ''' <summary>
    ''' Looks for the passed in string in the grid starting search in current cell. The search moves down current
    ''' column, then moves to the top of the next one and continues search down. Continues to search until reaching
    ''' the end of the grid or until the string is found. When the search string is found, the cell containing the
    ''' string is selected and searching stops.
    ''' </summary>
    ''' <param name="astrSearchString"></param>
    ''' <remarks></remarks>
    Private Sub FindNextStringInGrid(ByVal astrSearchString As String)

        Dim bolResultFound As Boolean = False 'flag to see if something was found

        Try
            'make sure the grid has records to move through
            If dgvResults.Rows.Count > 0 Then

                'make sure search string is not blank
                If astrSearchString.Trim <> "" Then

                    'get row and column indexes of current cell
                    Dim intColCurrentIndex As Integer = dgvResults.CurrentCell.ColumnIndex
                    Dim intRowCurrentIndex As Integer = dgvResults.CurrentCell.RowIndex

                    'loop through the columns
                    For Each col In dgvResults.Columns

                        'only search columns that contain or are to the right of selected cell
                        If dgvResults.Columns.IndexOf(col) < intColCurrentIndex Then
                            'do not search this column
                        ElseIf dgvResults.Columns.IndexOf(col) = intColCurrentIndex Then

                            'only search from selected cell down
                            For Each row In dgvResults.Rows
                                If dgvResults.Rows.IndexOf(row) > intRowCurrentIndex Then
                                    If row.Cells(dgvResults.Columns.IndexOf(col)).Value.ToString.ToLower.Contains(astrSearchString.Trim.ToLower) Then
                                        'select cell when string is found
                                        dgvResults.CurrentCell = dgvResults.Rows(dgvResults.Rows.IndexOf(row)).Cells(dgvResults.Columns.IndexOf(col))
                                        bolResultFound = True
                                        'if a result was found then no longer need to continue looping through rows
                                        Exit For
                                    End If
                                End If
                            Next

                        ElseIf dgvResults.Columns.IndexOf(col) > intColCurrentIndex Then

                            'search entire column
                            For Each row In dgvResults.Rows
                                If row.Cells(dgvResults.Columns.IndexOf(col)).Value.ToString.ToLower.Contains(astrSearchString.Trim.ToLower) Then
                                    'select cell when string is found
                                    dgvResults.CurrentCell = dgvResults.Rows(dgvResults.Rows.IndexOf(row)).Cells(dgvResults.Columns.IndexOf(col))
                                    bolResultFound = True
                                    'if a result was found then no longer need to continue looping through rows
                                    Exit For
                                End If
                            Next
                        End If

                        'if a result was found then no longer need to continue looping through columns
                        If bolResultFound = True Then
                            Exit For
                        End If
                    Next

                    If bolResultFound = False Then
                        MessageBox.Show("Finished searching end of results.", cAppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Else
                    MessageBox.Show("Please enter search text.", cAppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub


Tuesday, January 13, 2015

VB.NET - Export Excel to PDF

Imports System
Imports System.IO
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel


    ''' <summary>
    ''' converts excel spreadsheet to PDF
    ''' </summary>
    ''' <param name="strXLFile">file path of excel spreadsheet</param>
    ''' <param name="strPDF">file path of where to save the pdf</param>
    ''' <remarks></remarks>
    Public Shared Sub ConvertToPDF(ByVal strXLFile As String, ByVal strPDF As String)

        Dim objExcel As Excel.Application
        Dim objWB As Excel.Workbook

        Try

            'Open Excel and the workbook
            objExcel = New Application
            objExcel.Visible = False
            objWB = objExcel.Workbooks.Open(strXLFile)
            objWB.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, strPDF, , , , , , False)

        Catch ex As Exception
            Throw ex
        Finally
            If objWB IsNot Nothing Then
                objWB.Close()
            End If

            NAR(objWB)

            If objExcel IsNot Nothing Then
                objExcel.Quit() ' Need to leave application open for display.
            End If

            NAR(objExcel)
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try

    End Sub


    ''' <summary>
    ''' Used to properly clean up excel or powepoint COM objects.
    ''' </summary>
    ''' <param name="o"></param>
    ''' <remarks></remarks>
    Shared Sub NAR(ByVal o As Object)
        Try
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o)
        Catch

        Finally
            o = Nothing
        End Try
    End Sub