Quantcast
Channel: Oracle, MySQL, Sybase, Informix and other databases
Viewing all articles
Browse latest Browse all 1350

Calling and executing a stored procedure from sql into a vb.net textbox

$
0
0

I have created the following stored procedure on SQL server:
CREATE Procedure CalcTotal

@InvoiceID Varchar(4)

As Declare @Total int Begin SElect @Total = (TransportFee + EquipmentFee + LabourFee) From Invoice where InvoiceID = @invoiceID update Invoice set Total = @Total Where InvoiceID = @invoiceID End

My codes for my form on Visual Basic are: Imports System.Data.Sql Imports System.Data.SqlClient Public Class Invoice Dim sqlConn As New SqlConnection With {.ConnectionString = "Data Source=Hamsheed;Initial Catalog=assignment;Integrated Security=True"} Dim sqlstr As String = "Select * From Invoice" Dim MaxRows As Integer Dim RowIndex As Integer = 0 Dim dt As New DataTable

Private Sub Invoice_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim DataAdapter As New SqlDataAdapter(sqlstr, sqlConn)
    DataAdapter.Fill(dt)
    DataAdapter.Dispose()
    MaxRows = dt.Rows.Count
    InvoiceDetails()
    cmbSearch.DataSource = dt
    cmbSearch.DisplayMember = "FirstName"

End Sub

Sub InvoiceDetails()

    txtInvoiceID.Text = CStr(dt.Rows(RowIndex)("InvoiceID"))
    txtClientID.Text = CStr(dt.Rows(RowIndex)("ClientID"))
    txtEmployeeID.Text = CStr(dt.Rows(RowIndex)("EmployeeID"))
    txtFillInDate.Text = CStr(dt.Rows(RowIndex)("FillInDate"))
    txtTransportFee.Text = CStr(dt.Rows(RowIndex)("TransportFee"))
    txtEquipmentFee.Text = CStr(dt.Rows(RowIndex)("EquipmentFee"))
    txtLabourFee.Text = CStr(dt.Rows(RowIndex)("LabourFee"))
    txtTotal.Text = CStr(dt.Rows(RowIndex)("Total"))
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


    Dim InvoiceID As String = txtInvoiceID.Text
    Dim ClientID As String = txtClientID.Text
    Dim EmployeeID As String = (txtEmployeeID.Text)
    Dim FillInDate As Date = CDate(txtFillInDate.Text)
    Dim TransportFee As Integer = CInt(txtTransportFee.Text)
    Dim EquipmentFee As Integer = CInt(txtEquipmentFee.Text)
    Dim LabourFee As Integer = CInt(txtLabourFee.Text)
    Dim Total As Integer = CInt(txtTotal.Text)




    If CStr(dt.Rows(RowIndex)("paymentmethod")) = "Card" Then

        radCard.Select()

    ElseIf CStr(dt.Rows(RowIndex)("paymentmethod")) = "Cash" Then
        radCash.Select()

    Else
        radCredit.Select()
    End If


    Dim sqlQuery As String = ("Exec Insert_Invoice @InvoiceID = ' " & InvoiceID & " ', @ClientID = '" & ClientID & "',@EmployeeID ='" & EmployeeID & "', @FillInDate ='" & FillInDate & "',@TransportFee='" & TransportFee & "',@EquipmentFee = '" & EquipmentFee & "',@LabourFee= '" & LabourFee & "',@Total= '" & Total)

    Dim sqlcmnd As New SqlCommand(sqlQuery, sqlConn)

    Try
        sqlConn.Open()
        Dim changes As Integer = sqlcmnd.ExecuteNonQuery()
        sqlConn.Close()
        MessageBox.Show(changes & "Changes Made")

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Save()

End Sub


Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
    dt.Rows(RowIndex)("InvoiceID") = CStr(txtInvoiceID.Text)
    dt.Rows(RowIndex)("ClientID") = CStr(txtClientID.Text)
    dt.Rows(RowIndex)("EmployeeID") = CStr(txtEmployeeID.Text)
    dt.Rows(RowIndex)("FillInDate") = CStr(txtFillInDate.Text)
    dt.Rows(RowIndex)("TransportFee") = CStr(txtTransportFee.Text)
    dt.Rows(RowIndex)("EquipmentFee") = CStr(txtEquipmentFee.Text)
    dt.Rows(RowIndex)("LabourFee") = CStr(txtLabourFee.Text)
    dt.Rows(RowIndex)("Total") = CStr(txtTotal.Text)
    Dim sqlquery As String = "exec edit_invoice '" & txtInvoiceID.Text & "', " & txtLabourFee.Text & ", " & txtTransportFee.Text & ", " & txtEquipmentFee.Text & ", '" & txtpaymentMethod.Text & "', '" & txtClientID.Text & "', '" & txtEmployeeID.Text & "', '" & txtFillInDate.Text & "', '" & txtTotal.Text & "'"

    Dim sqlcmnd As New SqlCommand(sqlquery, sqlConn)

    Try
        sqlConn.Open()
        Dim changes As Integer = sqlcmnd.ExecuteNonQuery()
        sqlConn.Close()
        MessageBox.Show(changes & "Changes Made")

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Save()

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    txtInvoiceID.Clear()
    txtClientID.Clear()
    txtEmployeeID.Clear()
    txtFillInDate.Clear()
    txtTransportFee.Clear()
    txtEquipmentFee.Clear()
    txtLabourFee.Clear()
    txtTotal.Clear()



End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    If RowIndex <> MaxRows - 1 Then
        RowIndex += 1
        InvoiceDetails()
    End If
End Sub

Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
    RowIndex = MaxRows - 1
    InvoiceDetails()

End Sub


Private Sub btnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
    SecretaryMainMenu.Show()
    Me.Hide()


End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

    Dim invoiceID As String = txtInvoiceID.Text
    Dim SqlQuery As String = ("EXECUTE Delete_Invoice @InvoiceID = '" & InvoiceID & "'")
    Dim cmd As New SqlCommand(SqlQuery, sqlConn)
    Try
        sqlConn.Open()
        Dim changes As Integer = cmd.ExecuteNonQuery()
        sqlConn.Close()
        MessageBox.Show(changes & "Record Deleted")

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Save()

    dt.Rows(RowIndex).Delete()

End Sub

Sub Save()

    Dim dataAdapter As New SqlDataAdapter(sqlstr, sqlConn)
    Dim commandbuilder As New SqlCommandBuilder(dataAdapter)
    dataAdapter.Update(dt)
    dataAdapter.Dispose()

End Sub

Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
    Dim searchitem As String = InputBox("Input Search invoice ID: ", "Search by ID")
    Dim sresult As Boolean = False
    searchitem = searchitem.Trim.ToUpper

    For i As Integer = 0 To MaxRows - 1
        If CStr(dt.Rows(i)("invoiceid")).ToUpper = searchitem Then
            sresult = True
            RowIndex = i
            InvoiceDetails()
        End If
    Next

    If sresult = False Then
        MsgBox("No result found", MsgBoxStyle.OkOnly, "Information")
    End If
End Sub

Private Sub txtinvoice_validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtInvoiceID.Validating
    If Not txtInvoiceID.Text Like "I###" Then
        e.Cancel = True
        ErrorProvider1.SetError(txtInvoiceID, "Improper ID format")

    End If
End Sub

Private Sub txtinvoice_validated(ByVal sender As Object, ByVal e As EventArgs) Handles txtInvoiceID.Validated
    ErrorProvider1.SetError(txtInvoiceID, "")
End Sub


Now I want to call the stored procedure into the textbox Total, which will compute the 'transportFee, EquipmentFee and Labourfee' costs.. and display the total into the textbox of txtTotal.Text

How do I write that piece of code? URGENT HELP PLEASE !




Viewing all articles
Browse latest Browse all 1350

Trending Articles