?
Solved

Want to use a class level public function in more than one program

Posted on 2014-11-11
10
Medium Priority
?
142 Views
Last Modified: 2014-11-12
Hi Experts,

I have a public function to fill a grid.  I pass the Datagridview name and a query as parameter to this function.
This function is inside a class. How can I use this function in more than one program? At this time, when Prog1 call the function it can fill the grid1. But when Prog1 is open and Prog2 tries to call the function then grid2 is empty.  When I close prog1 and try to fill grid2 in prog2 then it works. So how can I use a function at the same time in more than one program.

Thanks in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40435904
Put the function into a dll and each program can reference that dll.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40435943
Is Prog1 and Prog2 in two different executable files? For example Prog1.exe and Prog2.exe or are they two different classes within the same executable file?
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40437822
Thank you for your response. I have not created the EXE yet. My project is "PanelASRS". Under this project I have two forms. "frmInProcess" and "frmOutProcess". Both are filling grids. So I wrote a function to fill these grids. This is my code and it is in a class file.

Public shared Function fillGrid(GridviewName As DataGridView, Sql As String) As Integer
        fillGrid = 0
        Try
            dt.Clear()
            If SQLComms.ConnectToDB(sqlConnectionString) = True Then
                cmd.Connection = SQLConn
                cmd = New SqlCommand(Sql, SQLConn)
                cmd.CommandText = Sql
                da.SelectCommand = cmd
                da.Fill(dt)
                bsource.DataSource = dt
                GridviewName.DataSource = bsource
                fillGrid = dt.Rows.Count
                SQLConn.Close()

            End If

        Catch ex As Exception
            MsgBox("Error while binding the Datagrid in PopulateGridView sub routine : " & ex.Message)
        End Try

    End Function
0
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40437844
This was your question title:
Want to use a class level public function in more than one program

Are you now saying you only want to call the function within the one program?  What is the problem?  A function can be called anywhere in a program - so long as the required permissions (eg. public - as you have) are in place.  Note in your sample you have da and dt which are not declared.  Be careful you do not end up with the grids being linked to the same data unless that is what you want.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40437896
It looks like you are using the same SqlDataAdapter and DataTable objects which are already in use when you try and fill the second data grid.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40438006
Right...What is the best way to do it?  because I need to fill grid so many times in my project. I need a common function. Can you please suggest some better way?

Thank you.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 40438011
You could pass those objects into the function as well as your current parameters.  
eg.
Public shared Function fillGrid(GridviewName As DataGridView, Sql As String, da as ???, dt as ???) As Integer
replace the ??? with the correct type of variable you require.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40438091
I will try and get back to you. Thank you for your help.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40438106
Will you be updating the database with the data that is being displayed from the DataGridView or is it just displaying info to the user.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40438239
I guess no credit to the person who found the error in your code....
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question