question

VAer-4038 avatar image
0 Votes"
VAer-4038 asked VAer-4038 edited

Calling method error

I have asked calling method before, and the thread gets messy (disorganized). Now I am starting a new thread with new issue.

I put public void InsertActivity() in public class GlobalMethod , which does not belong to any form.

In Form1, I try to call the method, but it does not recognize GlobalMethod.InsertActivity() ? What did I write wrong?

I don't really have programming knowledge, I am learning by doing. Feel like learning a lot in the past few days by solving one issue at a time.

Thanks.

     public class GlobalMethod  //It is just a class, not belong to any forms.
     {
         public static string Activity;
         public void InsertActivity()
         {
             OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);
             Cn.Open();            
    
             using (OdbcCommand cmd = new OdbcCommand("INSERT into [TableActivity](Username, EasternTime, Note) Values(@Username, @Time, @Note)", Cn))
             {
                 DateTime dtLocal = DateTime.Now;
                 TimeZoneInfo tziEST = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
                 DateTime dtEastern = TimeZoneInfo.ConvertTime(dtLocal, tziEST);
    
                 cmd.Parameters.Add("@Username", OdbcType.VarChar).Value = Environment.UserName;
                 cmd.Parameters.Add("@Time", OdbcType.DateTime).Value = dtEastern;
                 cmd.Parameters.Add("@Note", OdbcType.VarChar).Value = GlobalMethod.Activity;
             }
         }
     }
    
    
         private void Form1ButtonLogin_Click(object sender, EventArgs e)
         {
             GlobalMethod.Activity = "You log in.";
             GlobalMethod.InsertActivity();   
         }
windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered VAer-4038 edited

Hi VAer-4038,
You need to change your InsertActivity method to a static method and then you can call it by using the class name.
More details you can refer to this document.

 public static void InsertActivity()
 {
   //....
 }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I changed public static void InsertActivity(), but has error for globalmethod.InsertActivity(); as you mentioned in another post.

It says: Member of 'GlobalMethod.InsertActivity()' cannot be accessed with instance reference; qualify it with a type name instead.


Yes I do write globalmethod.InsertActivity(), but error is about GlobalMethod.InsertActivity()

If I use GlobalMethod.InsertActivity(); directly, it can compile but not recording anything. Did I write public static void InsertActivity() correctly?

Thanks.

    private void Form1ButtonLogin_Click(object sender, EventArgs e)
      {
           GlobalMethod.Activity = "You log in.";
            
           GlobalMethod globalmethod = new GlobalMehod();   //glodmethod is the name of instantiated object that you can customize 
           globalmethod.InsertActivity();
      }
0 Votes 0 ·

Hi @VAer-4038
When you change InsertActivity method to a static method, you can call it by using the class name"GlobalMethod".
Code likes below:

 private void Form1ButtonLogin_Click(object sender, EventArgs e)
 {
            GlobalMethod.Activity = "You log in.";
            GlobalMethod.InsertActivity();
               
 }
    
 class GlobalMethod
 {
       public static string Activity;
        public static void InsertActivity()
         {
            //.....
          }
 }

>>If I use GlobalMethod.InsertActivity(); directly, it can compile but not recording anything.
This means that the problem is in the code within your InsertActivity method, I suggest you check your code by breaking point.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Ok, I mistakenly thought I need to make both of below change. What did I write InsertActivity wrong? Why I did not find any record inserted into database? I think I also forgot Cn.Close(0 for method InsertActivity

public static void InsertActivity()

GlobalMethod globalmethod = new GlobalMehod(); //glodmethod is the name of instantiated object that you can customize
globalmethod.InsertActivity();

0 Votes 0 ·

By the way, how can I write below code in class, so that it can be shared with other forms. In another post, you teach me how to write code(validating textbox key press value) in class, and the code is exactly same.

But below code comes with customized variable name for DataGridView, in this case, it is dgvChildForm1 . How can I modify below code and put in class (outside any form)? So that many other forms can call the method. How do I do it while each DataGridView has different name?

Thanks.

Due to 1000 character limit, here is Google drive link: 1uNw7f-quzSVy39oUhbnmB189MInsfEt9


0 Votes 0 ·

Hi @VAer-4038
The btnExportToExcel_Click is an event not a method. If you wan t to call it in each form, I suggest you change it to a method.
Then you can pass the DataGridView as a variable parameter to the method. When each form calls it, pass the DataGridView in the form as a parameter.

 private void btnExportToExcelClick(DataGridView dgv)
 {
    ...
    dgv.ClearSelection();
 }
 private void copyAlltoClipboard(DataGridView dgv)
 {
     //Copy title row (Field name)
     dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
     dgv.SelectAll();
     DataObject dataObj = dgv.GetClipboardContent();
     if (dataObj != null)
         Clipboard.SetDataObject(dataObj);
 }

Best Regards,
Daniel Zhang

0 Votes 0 ·