I've been working on a project, but have decided to take another direction on it, after much and continued struggling.
I started out trying to use data binding, but was advised to use straight SQL statements in the code. So what I'm trying to do now: I have 2 forms, frm1 and frm2. frm1 has a combobox, which is showing all the data fine from a table, and an OK button. Upon clicking on this button, I need frm2 to load, correctly populating its textboxes and comboboxes with the corresponding data, based on the selection made on frm1. This is what I have so far:
Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports System.Windows.Forms Public Class frmLocations Dim conn As New SqlConnection("Data Source=f03d3s-dev01; Initial Catalog=dos_track;User Id=vbuser; Password=tran3;") Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn.Open() Call FillCombo() End Sub Private Sub FillCombo() Dim sql As String = "Select distinct Site,depot_refnbr From Locations order by Site asc" Dim cmd As New SqlCommand(sql, conn) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet Dim dt As New DataTable da.Fill(ds, "Locations") Dim dr As SqlDataReader = cmd.ExecuteReader Do While dr.Read cboLocations.Items.Add(dr("Site")) Loop dr.Close() cmd.Cancel() cmd.Dispose() End Sub Private Sub cboLocations_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLocations.SelectedIndexChanged End Sub Private Sub btnLocationOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocationOk.Click clsMain.Location_str_value = cboLocations.Text 'clsMain.Location_depot_refnbr = cboDepot. Dim f As New frmLocationInfo f.ShowDialog() End Sub
I also have a stored procedure to pass the desired value, depot_refnbr to frm2:
ALTER PROCEDURE dbo.sp_CorrespDepot @CorrespDepot varchar(50) output AS /* SET NOCOUNT ON */ SELECT DISTINCT depot.depot_name, depot.depot_refnbr FROM depot INNER JOIN locations ON depot.depot_refnbr = locations.depot_refnbr RETURN
Public Class frmLocationInfo Dim conn As New SqlConnection("Data Source=f03d3s-dev01; Initial Catalog=dos_track;User Id=vbuser; Password=tran3;") Private Sub frmLocationInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conn.Open() Call PopulateTextboxes() Call FillDepotCombo() End Sub Private Sub PopulateTextboxes() End Sub Private Sub FillDepotCombo() Dim sql As String = "Select distinct depot_name From Depot order by depot_name asc" Dim dtDepot As New DataTable("Depot") Dim cmdCorrespDepot As New SqlCommand("sp_CorrespDepot", conn) cmdCorrespDepot.CommandType = Data.CommandType.StoredProcedure cmdCorrespDepot.Parameters.Add("@CorrespDepot", SqlDbType.VarChar, 50) With cmdCorrespDepot .Parameters.Add("@CorrespDepot", SqlDbType.VarChar).Direction = ParameterDirection.InputOutput End With dtDepot.Load(cmdCorrespDepot.ExecuteReader()) cboDepot.DataSource = dtDepot cboDepot.DisplayMember = "depot_name" cboDepot.ValueMember = "depot_refnbr" conn.Close() End Sub End Class
I also have a clsMain, in where I keep global variables. When debugging, after clicking on the OK button on frm1, I keep receiving the error, "String: the Size property has an invalid size of 0". Can someone please advise on this?