Why it cause error (under the same namespece)

BenTam 1,801 Reputation points
2024-01-03T08:51:30.19+00:00

Dear All,

The codes are under the same namespace. Why that it raise an error?

Student_Form

SqlConn

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-01-03T09:42:02.72+00:00

    Hi @BenTam-3003 , Welcome to Microsoft Q&A,

    Your usage is wrong.

    Instantiation of the SqlConnectn class should use new SqlConnectn().

    The method name of the SqlConnectn class should be SqlConnect.

    In the Program class, you need to use an instance of the SqlConnectn class to call the SqlConnect method. Try to use this:

    namespace xxx
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                SqlConnectn conn=new SqlConnectn();
                SqlConnection sqlConnection = conn.SqlConnect();
                
    
            }
        }
        public class SqlConnectn
        {
           public SqlConnection SqlConnect()
            {
                string connectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
                SqlConnection Conn = new SqlConnection(connectionString);
                return Conn;
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,611 Reputation points
    2024-01-03T08:54:51.1233333+00:00
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.