Friday, 16 June 2017

View



conn = New SqlConnection(ConnectionStr)
conn.Open()

        If CboDesc.Text <> "" Then
            DescId = CInt(Mid(CboDesc.Text, (InStrRev(CboDesc.Text, "|") + 1), Len(CboDesc.Text)))
        Else
            DescId = 0
        End If

        sql = " Select a.Type,a.Description,b.DescId,b.Rate,b.EffTo,b.EffFrom from DescriptionMaster a " _
            & " Inner join (Select DescId,Rate,EffFrom,EffTo,LocationId," _
            & " ROW_NUMBER() OVER (PARTITION BY DescId ORDER BY EffFrom DESC) rn " _
            & " from RateMaster where Locationid = " & LocationId & ") b On a.TranId = b.DescId And rn = 1 " _
            & " Where  b.LocationId = " & LocationId & " "
        '& " Group By a.Type,a.Description,b.DescId,b.Rate,b.EffTo,b.EffFrom "

        If CboType.Text <> "" Then
            sql = sql + "And a.Type = '" & CboType.Text & "'"
        End If

        If DescId <> 0 Then
            sql = sql + "and "
        End If

        If DescId <> 0 Then
            sql = sql + "b.DescId = " & DescId & ""
        End If

        sql = sql + "Group By a.Type,a.Description,b.DescId,b.Rate,b.EffTo,b.EffFrom "

        Cmd = New SqlCommand(sql, conn)
        DR = Cmd.ExecuteReader

        DGVDesc.Rows.Clear()
        If DR.HasRows Then
            While DR.Read
                DGVDesc.Rows.Add()
                n = DGVDesc.RowCount - 1
                DGVDesc.Rows.Item(n).Cells("Type").Value = DR.Item("Type")
                DGVDesc.Rows.Item(n).Cells("Description").Value = DR.Item("Description") & "|" & DR.Item("DescId")
                DGVDesc.Rows.Item(n).Cells("Rate").Value = DR.Item("Rate")
                DGVDesc.Rows.Item(n).Cells("EffFrom").Value = DR.Item("EffFrom")
                DGVDesc.Rows.Item(n).Cells("Effto").Value = DR.Item("EffTo")
                End While
        Else
            MsgBox("No Records Found. Please Check...")
            End If
        DR.Close()

Monday, 12 June 2017

Text Box - Numeric Value

Private Sub TbCastingWt_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TbCastingWt.KeyPress
        If Asc(e.KeyChar) <> 13 And Asc(e.KeyChar) <> 8 And Asc(e.KeyChar) <> 46 And Not IsNumeric(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

Mail() with Attachment

 Public Sub Mail()
        Dim Sql1 As String
        Dim Conn1 As SqlConnection
        Dim Cmd1 As SqlCommand
        Dim DR1 As SqlDataReader

        Conn1 = New SqlConnection(ConnectionStr)
        Conn1.Open()

        Sql1 = "Select * from FeasChecklistMaster a   " _
        & " Inner Join FeasChecklistDetail b on a.tranMasId = b.TranMasId  and b.approved = 'A' and b.FeasReviewapp = 'A' " _
        & " Where EnquiryNo = '" & CboEnquiryNoApp.Text & "'  " _
        & " and a.Locationid = " & LocationId & " "

        Cmd1 = New SqlCommand(Sql1, Conn1)
        DR1 = Cmd1.ExecuteReader

        If DR1.HasRows Then
            While DR1.Read

                Using Conn As SqlConnection = New SqlConnection(ConnectionStr)

                    Conn.Open()

                    Dim transaction As SqlTransaction = Conn.BeginTransaction(IsolationLevel.ReadCommitted)

                    Dim command As SqlCommand = Conn.CreateCommand()

                    command.Transaction = transaction

                    Try
                        Dim FromAddress As String = Nothing
                        Dim ToAddress As String = Nothing
                        Dim CCAddress As String = Nothing
                        Dim BCCAddress As String = Nothing
                        Dim PartyToAddress As String = Nothing
                        Dim PartyCCAddress As String = Nothing

                        '  SelEnqNo = DR.Item("EnquiryNo")

                        EnquiryNo = DR1.Item("EnquiryNo")
                        ProdName = DR1.Item("ComponentName")
                        CustomerType = DR1.Item("CustomerType")

                        FrmEnggEstimationCostingReport.Sp_EnggEstimationMasTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationMas, LocationId, EnquiryNo, ProdName, CustomerType)
                        FrmEnggEstimationCostingReport.Sp_CompanyDetailTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_CompanyDetail, LocationId)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationCoreBoxTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationCoreBox, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationCoreTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationCore, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationCoreAssemplyTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationCoreAssemply, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationSleeveTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationSleeve, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationDevelopmentCostTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationDevelopmentCost, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationDevelopmentTimeTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationDevelopmentTime, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationTestingTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationTesting, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_EnggEstimationRemarksTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_EnggEstimationRemarks, LocationId, EnquiryNo, ProdName)
                        FrmEnggEstimationCostingReport.Sp_GetUnitTableAdapter.Fill(FrmEnggEstimationCostingReport.DS_Main.Sp_GetUnit)
                        FrmEnggEstimationCostingReport.ReportViewer1.RefreshReport()

                        Dim mimeType As String = String.Empty
                        Dim encoding As String = String.Empty
                        Dim extension As String = "xls"

                        Dim bytes As Byte() = FrmEnggEstimationCostingReport.ReportViewer1.LocalReport.Render("Excel", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)

                        Dim oFileStream As System.IO.FileStream
                        Dim strFilePath As String = String.Empty
                        strFilePath = "C:\Temp\EnggEstimationCostingReport.xls"

                        oFileStream = New System.IO.FileStream(strFilePath, System.IO.FileMode.Create)
                        oFileStream.Write(bytes, 0, bytes.Length)
                        oFileStream.Close()

                        MailSend = 0

                        Dim Subject As String
                        Dim BodyText As String

                        Subject = "Engineering Estimation Costing Report For Your Enquiry No.:" + DR1.Item("EnquiryNo") + ""

                        BodyText = "Dear Sir/Madam, " + vbCrLf + vbCrLf _
                                 + "For your reference with this mail, we have attached the Engineering Estimation Costing Report For Your Enquiry No.:" + DR1.Item("EnquiryNo") + ". " + vbCrLf + vbCrLf _
                                 + "Please check and revert back us if any." + vbCrLf + vbCrLf _
                                 + "Note:  ***** This is a auto generated mail. Please reply to concerned person(s) ****" + vbCrLf + vbCrLf

                            FromAddress = "pppp@kkkk.com"
                            Dim SendFrom As MailAddress = New MailAddress(FromAddress)
                            ToAddress = " 1@kkkk.com,2@kkkk.com "
                            Dim SendTo As MailAddress = New MailAddress(ToAddress)
                       

                            CCAddress = "cc@kkkk.com"
                            Dim SendCC As MailAddress = New MailAddress(CCAddress)
                     

                        Dim mailInstance As MailMessage

                        Dim mailSenderInstance As SmtpClient = New SmtpClient("192.168.100.7", 25) '25 is the port of the SMTP host

                        mailSenderInstance.Credentials = New System.Net.NetworkCredential("systemadmin", "systemadmin")

                        mailInstance = New MailMessage(FromAddress, ToAddress, Subject, BodyText)

                        If CCAddress <> "" Then
                            mailInstance.CC.Add(CCAddress)
                        End If

                        Dim attachment As System.Net.Mail.Attachment
                        attachment = New System.Net.Mail.Attachment(strFilePath)
                        mailInstance.Attachments.Add(attachment)

                        mailInstance.Priority = MailPriority.High

                        mailSenderInstance.Send(mailInstance)

                        mailInstance.Attachments.Dispose()
                        mailInstance.Dispose() 'Please remember to dispose this object

                        MailSend = 1

                        '--------------------------------------------------------------------

                        If MailSend = 1 Then
                            transaction.Commit()
                            MsgBox("Mail Has Been Send Successfully For EnquiryNo No : " + DR1.Item("EnquiryNo") + "")
                            Me.Enabled = True
                        Else
                            transaction.Rollback()
                            DR1.Close()
                            Conn1.Close()
                            MsgBox(Err.Description)
                            MsgBox("Error while processing. Previous execution was rollback")
                            Me.Enabled = True
                            Exit Sub
                        End If

                    Catch ex As Exception
                        transaction.Rollback()
                        DR1.Close()
                        Conn1.Close()
                        MsgBox(Err.Description)
                        MsgBox("Error while processing. Previous execution was rollback")
                        Exit Sub
                    End Try

                End Using
            End While
        End If
        DR1.Close()
    End Sub