Results 1 to 6 of 6

Thread: [RESOLVED] Not sure how to call a function?

  1. #1
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    Resolved [RESOLVED] Not sure how to call a function?

    I am in the process of teaching myself the C# language and running into issues. As simple and ridiculous as this sounds I can't seem to call a function. I have created a class for all my DB function. Here is what it looks like:

    Code:
    using System;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        class DBUtilities
        {
            public SqlConnection dbConn()
            {
                SqlConnection cnn = new SqlConnection("Data Source=MCK\\MCKEXPRESS;Initial Catalog=myDataBase;Integrated Security=SSPI;");
    
                if (cnn == null && cnn.State == System.Data.ConnectionState.Open) 
                { 
                   cnn.Open();
                } 
    
                return cnn;
            }
        }
    }
    Now, I have a Windows Form in which I want to call the function dbConn() and I'm just not sure how to do it. I've tried googling it and not finding exactly what I'm looking for. Looking at the code above, if you know of a more efficient way to do this...I'm all ears!!!

    Thanks,
    Blake

  2. #2
    Frenzied Member kfcSmitty's Avatar
    Join Date
    May 05
    Location
    Kingston, Ontario
    Posts
    1,792

    Re: Not sure how to call a function?

    Since your class isn't static, you'll need to create an instance of it on your other form.

    C# Code:
    1. //create the instance
    2. DBUtilities dbutils = new DBUtilities();
    3. //call the function
    4. SqlConnection con = dbutils.dbConn();

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 05
    Location
    Philippines
    Posts
    10,279

    Re: Not sure how to call a function?

    Why are you checking if cnn is null? Perhaps what you need to do is to instantiate cnn once and just open and close it accordingly. And you are also checking if its state is open then why are you opening it? Did you miss an exclamation point (!) there?

  4. #4
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    Re: Not sure how to call a function?

    Thanks guys,

    That helped. Not sure why that post code checks for connectionState.Open. In my code, it actually shows connectionState.Closed. I must have forgot to change it in the post.
    Blake

  5. #5
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,434

    Re: Not sure how to call a function?

    even no need to check null value .the following is more simplified way .
    Code:
    using System;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        class DBUtilities
        {
            public SqlConnection dbConn()
            {
                SqlConnection cnn = new SqlConnection("Data Source=MCK\\MCKEXPRESS;Initial Catalog=myDataBase;Integrated Security=SSPI;");
    
                if (cnn.State == System.Data.ConnectionState.Open) 
                { 
                   cnn.Open();
                } 
    
                return cnn;
            }
        }
    }

  6. #6
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    Re: Not sure how to call a function?

    I actually changed my code to that after I looked at it more. The way I had it before, I had found as an example in open a sqlConnection.

    Thanks firoz.raj
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •