Execute another form's Sub from a Dialog

Hi,

I'm new at vb2005 and this is my problem:

Let's say I have a form with a Combobox filled from a dataset, and I want to show a dialog where the user might insert a new value for that Combobox. If the user hits the Ok button, the values are stored ok, but what would I have to do to reload the Combobox content as soon as the user hits ok in the dialog, so the new value appears on the list

Thanks in advance.

 

 



Answer this question

Execute another form's Sub from a Dialog

  • Andrew Mackie

    Okay problem is something else, did u try refresh all controls on the form try refresh the form where the combo lies, and even refresh the combo.



  • FrenchiInLA

    Well, it didn’t work for me but I found a solution: The function that fills combobox in form1 I named it as Public Shared (also I have to declare a new combobox as shared) and then if I call the shared function in form1 from form2 it works.

    Form1 code:

    Public shared xCombo as ComboBox 'Declaration

    xCombo = Combobox1
    'In the load section of the form

    Public Shared Sub FillCombo (bla bla)

    Dim x as new MeSQLAssembly.SQLQueries
    xCombo.datasource = x.FillCombo

    End Sub

    Form2 code:

    Call Form1.FillCombo

    And that's it, I hope it helps anyone who has this problem, it took me several hours to have it work

    Regards.


  • 愛青蛙

    Well, in fact I tried to clear Combobox datasource from dialog, but nothing happens, data just remains:

    xForm.cbGroup.datasource = nothing
    xForm.cbGroup.items.clear()


    So, I don't really what to do, I also tried to create a new datasource from the diaog and try to assign it to the Combobox, but is just hopeless:

    xForm.cbGroup.datasource = nothing
    xForm.cbGroup.items.clear()
    Dim xDS2 As New DSources.SqlQueries
    xForm.cbgroup.ValueMember =
    "idgroup"
    xForm.cbgroup.DisplayMember = "group"
    xForm.cbgroup.DataSource = xDS2.ProvideDS("Select * from groups", Conn).Tables(0)

    So everytime I insert a new value from the dialog, I have to quit the form where the Combobox resides, and call it again to have new values on it.

    Any idea



  • Indigo Paul

    I don't think you can add a new value to the combobox if the combobox has a datasource, bound to the dataset as you had explained. The only way to do so would be to add the new item directly into the datatable, adding a new row of data and filling in the appropriate fields, then rebinding the combobox, by again, calling the "DataSource = theDataSet.Tables(0).DefaultView" property.

    Thread moved to the appropriate forum



  • paramjeet

    I thank you all for your help, but I think I wasn't enough explicit, my mistake, but here goes:

    This is my function in a assembly that returns a dataset so the Combobox can be filled:

    Public Function ProvideDS(ByVal PSql As String, ByVal DBConn As SqlClient.SqlConnection) As DataSet

    Dim ASQL1 As New SqlDataAdapter(PSqlCadena, DBConexion)

            Try
                   ProdDataSet = New DataSet
                   ASQL1.Fill(ProdDataSet)
                   
    Return ProdDataSet

            Catch exc As Exception
                     MsgBox(exc.Message, MsgBoxStyle.Critical,
    "Error")
                     Return ProdDataSet
            Finally
                    
    ASQL1.Dispose()
                     ProdDataSet.Dispose()
             End Try

    End Function

    And this is how I fill the Combobox

    Dim xDS As New DSources.SqlQueries
    cbgroup.ValueMember =
    "idgroup"
    cbgroup.DisplayMember = "group"
    cbgroup.DataSource = xDS.xFillDataSources("Select * from groups", Conn).Tables(0)


    So, what would I have to do to refill Combobox with new values from database but being refreshed from a dialog box (not from where the Combobox resides), is it wrong the way I am doing things around here I accept hard critics :)

    Thanks.

     

     


  • MarcoB


    Dim
    strNew As String = "Insert into groups (group) Values ('" & newGroup.Text & "')"
    Dim NewGroup As New DSources.SqlQueries
    NewGroup.QryInsert(strNew,Conn)

    Function:

    Public
    Function QryInsert(ByVal InsertStr As String, ByVal DBConn As SqlClient.SqlConnection) As Boolean

    Dim ASQL1 As New SqlCommand(InsertStr,DBConn)

    Try
    ASQL1.ExecuteNonQuery()
    Return True

    Catch exc As Exception
    MsgBox(exc.Message, MsgBoxStyle.Critical,
    "Error")
    Return False
    Finally
    ASQL1.Dispose()
    End Try

    End Function


  • millie_w

    I think it is alright, having an object that hold all your methods to query , insert, update etc..

    coming to the updating of your combobox again, the emphasis should be in your dataset.,

    so basically your combo is bounded to ProdDataSet in your xDS object,

    Think of the combo as only a display thingi, and adds, updates, etc, are done in your background implementation of data access. if you handle these well, the combo will obey.:)

    you say you already have inserted values and it works., did u use insertcommand

    okay please post related code in that DSources.SqlQueries class.

    did u try something like inserting values in the combo and refresh post.

    okay



  • enric vives

    works fine, did u try something like in the red segment/

    Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.ComboBox1.DataSource = Me.ProvideDS("Select * from amounttypes", New SqlClient.SqlConnection(My.Settings.CostingConnectionString)).Tables(0)

    Me.ComboBox1.DisplayMember = "amounttype"

    End Sub

    Dim t As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim strNew As String = "Insert into amounttypes Values ('" & t.ToString & "')"

    t += 1

    QryInsert(strNew, New SqlClient.SqlConnection(My.Settings.CostingConnectionString))

    Me.ComboBox1.DataSource = Me.ProvideDS("Select * from amounttypes", New SqlClient.SqlConnection(My.Settings.CostingConnectionString)).Tables(0)

    End Sub

    Public Function QryInsert(ByVal InsertStr As String, ByVal DBConn As SqlClient.SqlConnection) As Boolean

    Dim ASQL1 As New SqlClient.SqlCommand(InsertStr, DBConn)

    DBConn.Open()

    Try

    ASQL1.ExecuteNonQuery()

    DBConn.Close()

    Return True

    Catch exc As Exception

    DBConn.Close()

    MsgBox(exc.Message, MsgBoxStyle.Critical, "Error")

    Return False

    Finally

    ASQL1.Dispose()

    End Try

    End Function

    Public Function ProvideDS(ByVal PSql As String, ByVal DBConn As SqlClient.SqlConnection) As DataSet

    Dim ProdDataSet As New DataSet

    Dim ASQL1 As New SqlClient.SqlDataAdapter(PSql, DBConn)

    Try

    ASQL1.Fill(ProdDataSet)

    Return ProdDataSet

    Catch exc As Exception

    MsgBox(exc.Message, MsgBoxStyle.Critical, "Error")

    Return ProdDataSet

    Finally

    ASQL1.Dispose()

    ProdDataSet.Dispose()

    End Try

    End Function

    End Class



  • dreameR.78

    I'm sorry, it was my mistake here goes:

    Public Function ProvideDS(ByVal PSql As String, ByVal DBConn As SqlClient.SqlConnection) As DataSet

    Dim ASQL1 As New SqlDataAdapter(PSql, DBConn)

    Try
    ProdDataSet = New DataSet
    ASQL1.Fill(ProdDataSet)
    Return ProdDataSet

    Catch exc As Exception
    MsgBox(exc.Message, MsgBoxStyle.Critical,
    "Error")
    Return ProdDataSet
    Finally
    ASQL1.Dispose()
    ProdDataSet.Dispose()
    End Try

    End Function

    And this is how I fill the Combobox

    Dim xDS As New DSources.SqlQueries
    cbgroup.ValueMember =
    "idgroup"
    cbgroup.DisplayMember = "group"
    cbgroup.DataSource = xDS.ProvideDS("Select * from groups", Conn).Tables(0)

    Where "Conn" is a global variable where I store the database connection so I can use it everywhere

    Do you think there's a better way to do this , what about modifying the combobox outside the form (dialog)

    Thanks


  • Keithyboy1

    I think the dailog should be a member of your main form.

    Suppose that your main form is Form MainForm, while your dailog is MyDialog MyDialog1;

    //Add a public member to class MyDialog

    public DataSet ds;

    //in main form

    MyDialog MyDialog1= new MyDialog();

    MyDialog1.ds = this.ds; //pass the data set bound to your combo box

    MyDialog1.Show();

    //rebind the data source after adding new row

    Combobox1.datasource = null;

    Combobox1.datasource = ds;

    //In dailog

    private void buttonOK_click(object sender, system.eventargs e)

    {

    //Add new row to data set
    }



  • Ahmad Mageed

    the only difference here is i've put ur functions on the same form, doesnt matter!

  • mrshrinkray

    cbgroup.DataSource = xDS.xFillDataSources("Select * from groups", Conn).Tables(0)

    okay you did not send the appropriate functions if im right, PROVIDEDS is called where plus your code is wrong here. : ,conn) and xfilldatasources is

    Dim xDS As New DSources.SqlQueries is returning a datasource here. while u precise dataset is returned by provideds.

    please be clear. Thanks



  • TurboTom

    George Waters wrote:

    but what would I have to do to reload the Combobox content as soon as the user hits ok in the dialog, so the new value appears on the list

    Thanks in advance.

    You need to refresh to source to which you combobox is binded when ok is pressed , after you stored your value in the table ( in the database) ,.

    for example:

    me.BillTableAdapter.Update(me.CostingDataSet.Bill)

    if the combo is binded to me.CostingDataSet.Bill

    okay so if ur combo box is binded, thats the way.. your dataset which is virtual needs to be refreshed, when you do updates or inserts on the database itself.



  • Execute another form's Sub from a Dialog