Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:44:32+00:00 2026-06-04T07:44:32+00:00

i have the following function in Form1 and would like to call it when

  • 0

i have the following function in Form1 and would like to call it when a button in Form2 is clicked.

   public object GetStudents()
    {
        using (var DB = new myView1())
        {
            var studentList = (from s in DB.View123
                            select new { s.StudentName, s.StudentNumber, s.ClassName, s.StreamName, s.ParentName }).ToList();
            dataGridViewStudents.DataSource = students;
            return students.ToList();
        }
    }

Above function is in Form1, how do i call it from a button_Click event in Form2. This is some thing simple in VB.NET

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:44:33+00:00Added an answer on June 4, 2026 at 7:44 am

    It’s also simple in C#. You just need an instance of Form1 and then you can call this method on that instance.

    However, there’s a better approach. This code doesn’t actually belong on a form. Forms are for UI interactions, not for business logic and data access. You’re better off moving this code to a common location which would be accessed by both forms.

    Create a data access class. Something like this:

    public class StudentRepository
    {
        public static object GetStudents()
        {
            using (var DB = new myView1())
            {
                var studentList = (from s in DB.View123
                            select new { s.StudentName, s.StudentNumber, s.ClassName, s.StreamName, s.ParentName }).ToList();
                dataGridViewStudents.DataSource = students;
                return students.ToList();
            }
        }
    }
    

    Notice also that the method is now static which means it doesn’t need an instance to be called. (This is like the Shared keyword in VB.) So your forms can just call:

    var students = StudentRepository.GetStudents();
    

    Another thing you’ll want to fix, although it’s outside the scope of this question, is that return type. object isn’t very specific. You should have a Student class and that method should return IList<Student>. Something like this:

    public class Student
    {
        public string StudentName { get; set; }
        public int StudentNumber { get; set; }
        public string ClassName { get; set; }
        public string StreamName { get; set; }
        public string ParentName { get; set; }
    }
    

    and:

    public class StudentRepository
    {
        public static IList<Student> GetStudents()
        {
            using (var DB = new myView1())
            {
                var studentList = (from s in DB.View123
                            select new Student { StudentName = s.StudentName, StudentNumber = s.StudentNumber, ClassName = s.ClassName, StreamName = s.StreamName, ParentName = s.ParentName }).ToList();
                dataGridViewStudents.DataSource = students;
                return students.ToList();
            }
        }
    }
    

    I made some assumptions about the types for the fields, you should be able to correct those if they’re wrong. You might even want it to return an IEnumerable<Student> instead of IList<Student> if it’s an unchanging result (that is, if it’s just an enumeration of students and not a list to be added to, removed from, etc.).

    There’s a lot more you can do here, such as having an object for other data elements such as Class (which might need a different name, just to be clean), Parent, Stream, etc. (Come to think of it, several of these might need better names. Your variable naming in general needs a little work. Things like Form1 or myView1 don’t convey intent very well.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Public Class Form1 Private Function Step1_Execute() As Boolean Return
Hi I have the following script in my form function pdf() { var frm
I have the following Symfony2 form: public function buildForm(FormBuilder $builder, array $options) { $builder
I have the following code: var formSubmitHandler = function (link, form) { //e.preventDefault(); var
I have the following script: <script> (function(d, s, id, p) { var js, ins
I have the following code: $('#DoButton').click(function (event) { event.preventDefault(); $(input:checked).each(function () { var id
What I would like to do is the following. I have an image which
I have a strange one. Create a new form. Then add the following function
i have the following block of code: $(#contact_container form, #contact_details form).live( submit, function(event) {
i have following function but it is not working in IE. and please tell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.