Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > BOOK: Beginning Microsoft Visual Basic 2010
|
BOOK: Beginning Microsoft Visual Basic 2010
This is the forum to discuss the Wrox book Beginning Visual Basic 2010 by Thearon Willis, Bryan Newsome; ISBN: 9780470502228
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 11th, 2012, 10:34 PM
Authorized User
 
Join Date: Mar 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Visual Basic and SQL Server | Retrieving and Adding data.

This is a desperate plea for some help! I am lost!

I have a database in SQL Server containing two Stored Procedures: One reads all it's data to a Windows Form using textboxes in Visual Studio and the other is suppose to read all it's data into a Datagridview on the same form.

Here is where I am lost:

I am trying to take the REP_ID number displayed in the textbox from the first Stored Procedure called GetAllReps and feed it back to the other Store Procedure called GetSalaryByReps, finally I need to display the result of GetSalaryByReps on the DataGridview.

The GetSalaryByReps stored procedure requires an ID number which I must get form the textbox use to display the REP_ID on the form.

Here is my full code:

Code:
Imports System.Data
Imports System.Data.SqlClient

Public Class frmBindingData

    Dim objConnection As New SqlConnection _
        ("Server=test;Database=test;Trusted_Connection=True")
    Dim objDataAdapter As New SqlDataAdapter("EXEC GetAllReps", objConnection)
    Dim objDataSet As DataSet

    Dim objDataView As DataView
    Dim objCurrencyManager As CurrencyManager


    Private Sub FillDataSetAndView()
        ' Initialize a new instance of the DataSet object..
        objDataSet = New DataSet()
        ' Fill the DataSet object with data..
        objDataAdapter.Fill(objDataSet, "SALES_REP")
        ' Set the DataView object to the DataSet object..

        objDataView = New DataView(objDataSet.Tables("SALES_REP"))
        objDataView.Sort = "REP_ID ASC"
        objCurrencyManager = CType(Me.BindingContext(objDataView), CurrencyManager)
    End Sub


    Private Sub BindFields()
        ' Clear any previous bindings..
        txtIDNumber.DataBindings.Clear()
        txtFirstName.DataBindings.Clear()
        txtLastName.DataBindings.Clear()
        txtDateHire.DataBindings.Clear()
        txtCommision.DataBindings.Clear()

        ' Add new bindings to the DataView object.
        txtIDNumber.DataBindings.Add("Text", objDataView, "REP_ID")
        txtFirstName.DataBindings.Add("Text", objDataView, "FIRST_NAME")
        txtLastName.DataBindings.Add("Text", objDataView, "LAST_NAME")
        txtDateHire.DataBindings.Add("text", objDataView, "HIRE_DATE")
        txtCommision.DataBindings.Add("Text", objDataView, "COMMISION")
        ' Display a ready status..
        ToolStripStatusLabel1.Text = "Ready"
    End Sub


----------I started to create this to get the REP_ID from the text box into the Stored Procedure but I am not sure I am getting this done the right way.------------
    Private Sub rntRepId()
        objConnection.Open()
        Dim objCommand As SqlCommand = New SqlCommand()
        objCommand.Connection = objConnection
        objCommand.CommandType = CommandType.StoredProcedure
        objCommand.CommandText = "GetSalaryByReps"
        objCommand.Parameters.AddWithValue("@REP_ID", txtIDNumber.Text)
        objCommand.ExecuteNonQuery()
        objConnection.Close()
    End Sub
----------------------------------------------------------------------------


    Private Sub ShowPosition()
        'Always format the number in the txtPrice field to include cents
        Try
            txtCommision.Text = Format(CType(txtCommision.Text, Decimal), "c")
        Catch e As System.Exception
            txtCommision.Text = "0"
            txtCommision.Text = Format(CType(txtCommision.Text, Decimal), "c")
        End Try
        ' Display the current position and the number of records
        txtRecordPosition.Text = objCurrencyManager.Position + 1 & _
        " of " & objCurrencyManager.Count()
    End Sub


    Private Sub grpDataDisplay_Enter(sender As System.Object, e As System.EventArgs) Handles grpDataDisplay.Enter

    End Sub

    Private Sub StatusStrip1_ItemClicked(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles StatusStrip1.ItemClicked

    End Sub

    Private Sub frmBindingData_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' Fill the DataSet and bind the fields..
        rntRepId()
        FillDataSetAndView()
        BindFields()
        ' Show the current record position..
        ShowPosition()

    End Sub

    Private Sub btnMoveFirst_Click(sender As System.Object, e As System.EventArgs) Handles btnMoveFirst.Click
        ' Set the record position to the first record..
        objCurrencyManager.Position = 0
        ' Show the current record position..
        ShowPosition()
    End Sub

    Private Sub btnMovePrevious_Click(sender As System.Object, e As System.EventArgs) Handles btnMovePrevious.Click
        ' Move to the previous record..
        objCurrencyManager.Position -= 1
        ' Show the current record position..
        ShowPosition()

    End Sub

    Private Sub btnMoveNext_Click(sender As System.Object, e As System.EventArgs) Handles btnMoveNext.Click
        ' Move to the next record..
        objCurrencyManager.Position += 1
        ' Show the current record position..
        ShowPosition()
    End Sub

    Private Sub btnMoveLast_Click(sender As System.Object, e As System.EventArgs) Handles btnMoveLast.Click
        ' Set the record position to the last record..
        objCurrencyManager.Position = objCurrencyManager.Count - 1
        ' Show the current record position..
        ShowPosition()
    End Sub

    Private Sub txtRecordPosition_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtRecordPosition.TextChanged

    End Sub

    Private Sub ToolStripStatusLabel1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripStatusLabel1.Click

    End Sub

    Private Sub grdDataView_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdDataView.CellContentClick

    End Sub

    Private Sub grpNavigation_Enter(sender As System.Object, e As System.EventArgs) Handles grpNavigation.Enter

    End Sub
End Class
Thank you!

Last edited by NovicePGM2011; March 11th, 2012 at 10:40 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual Basic SQL Server 7.0 code aqua Beginning VB 6 3 July 17th, 2018 07:51 PM
How to use Visual Basic 6 connect to SQL server? mytreasure23 SQL Server 2000 2 August 31st, 2006 05:02 AM
Beginning SQL Server 2000 For Visual Basic Develop viper7 All Other Wrox Books 2 February 17th, 2005 02:07 PM
Visual Basic .NET and SQL Server 2000 rayap All Other Wrox Books 3 May 31st, 2004 09:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.