Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how can i minimized this code :
VB
cn.open()

Dim temp As String
       temp = "pending for client approval"

       str = "select Count(*)  from Asset_Service_Master where CaseStatus=  @temp "
       cmd = New SqlCommand(str, cn)
       cmd.Parameters.Add(New SqlParameter("@temp", temp))

       tb_PendingForClientApproval.Text = cmd.ExecuteScalar().ToString


       Dim tp As String
       tp = "yes"
       Dim tmp As String = ""

       str = "select count(*)  from Asset_Service_Master where CallClosed= @tp &  AND BillingComplete=@tmp   "
       cmd = New SqlCommand(str, cn)

       cmd.Parameters.Add(New SqlParameter("@tp", tp))
       cmd.Parameters.Add(New SqlParameter("@tmp", tmp))

       tb_CalledClosed.Text = cmd.ExecuteScalar().ToString

       cn.Close()
Posted
Updated 19-Sep-12 2:11am
v2

I don't see much more than:
VB
Dim tp As String  = "yes"

You can also remove a couple of superflous linebreaks.
Regards,

— Manfred
 
Share this answer
 
You have two blocks of code very similar, create a method (with proper arguments) and call it twice.

BTW you may also discard the SqlParameters, since you are using constants (write the constants directly inside the queries).
 
Share this answer
 
Comments
neldesai 19-Sep-12 8:36am    
is passing constant can protected me against sql injections..?Or i should go with parameterized..Thank u.
First thing there is nothing wrong with what you did.

But since you asked :)

First I would create a stored procedure
SQL
CREATE PROCEDURE [dbo].[Asset_Service_Master_Get_Counts]
    @CaseStatus VARCHAR(50) = NULL, --You'll need to add the correct types.
    @CallClosed VARCHAR(50) = NULL,
    @BillingComplete VARCHAR(50) = NULL
AS 
    BEGIN
        SELECT  COUNT(*)
        FROM    Asset_Service_Master
        WHERE   CaseStatus = COALESCE(@CaseStatus, Asset_Service_Master.CaseStatus)
                AND CallClosed = COALESCE(@CallClosed,Asset_Service_Master.CallClosed)
                AND BillingComplete = COALESCE(@BillingComplete,
                                               Asset_Service_Master.BillingComplete)
    END

GO


Here is the method I would use.
VB
Public Function GetCount(Optional ByVal caseStatus As String = "", Optional ByVal callClosed As String = "", Optional ByVal billingComplete As String = "") As String
    Using cn As New SqlConnection(connectionString)
        cn.Open()
        If cn.State = ConnectionState.Open Then
            Using cmd As New SqlCommand("Asset_Service_Master_Get_Counts", cn)
                cmd.Parameters.Add(New SqlParameter("@CaseStatus", IIf(caseStatus = "", DBNull.Value, caseStatus)))
                cmd.Parameters.Add(New SqlParameter("@CallClosed", IIf(callClosed = "", DBNull.Value, callClosed)))
                cmd.Parameters.Add(New SqlParameter("@BillingComplete", IIf(billingComplete = "", DBNull.Value, billingComplete)))
                cmd.CommandType = CommandType.StoredProcedure

                Return cmd.ExecuteScalar().ToString
            End Using
        Else
            Return String.Empty
        End If
    End Using
End Function
 
Share this answer
 
hii,

use this code


VB
Public Sub setvalue()
        Dim para(1) As SqlParameter
        para(0) = New SqlParameter("@temp", "pending for client approval")
        tb_PendingForClientApproval.Text = getValue("select Count(*)  from Asset_Service_Master where CaseStatus=  @temp ", para)


        Dim para_2(2) As SqlParameter
        para_2(0) = New SqlParameter("@tp", "yes")
        para_2(1) = New SqlParameter("@temp", "")

        tb_CalledClosed.Text = getValue("select count(*)  from Asset_Service_Master where CallClosed= @tp   AND BillingComplete=@tmp ", para_2)

    End Sub

    Public Function getValue(ByVal Query As string,ByVal para() As SqlParameter) As String)
        Try
            cn.open()
            cmd = New SqlCommand(Query, cn)
            cmd.Parameters.AddRange(para)
            Return cmd.ExecuteScalar().ToString

        Catch ex As Exception
        Finally
            If (Not cn.state = ConnectionState.Closed) Then
                cn.close()
            End If
        End Try
        Return ""
    End Function
 
Share this answer
 
may be your Question is not so clear
but
you can try like this also
cn.open()
 Dim tp As String = ""
        Dim tmp As String = "pending for client approval"
        If tmp = "pending for client approval" Then
            Str = "select Count(*)  from Asset_Service_Master where CaseStatus=  @temp "
        Else
            tp = "yes"
            tmp = ""
            Str = "select count(*)  from Asset_Service_Master where CallClosed= @tp &  AND BillingComplete=@tmp   "
        End If
        md = New SqlCommand(Str, cn)

        cmd.Parameters.Add(New SqlParameter("@tp", tp))
        cmd.Parameters.Add(New SqlParameter("@tmp", tmp))

        tb_CalledClosed.Text = cmd.ExecuteScalar().ToString

        cn.Close()

Declare All other Variables as per your Requirement
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900