C
C#2y ago
kevin

✅ Issues with setting up different classes and sql connection

Could someone help me understand how I can setup this SQL connection? it's my first time organizing my code into different folders and really structuring my app with different folders, etc. I get the error "A field initializer cannot reference the non-static field, method or property 'Customer.connection' ". I want to make it so that I have a connection class that handles the connection. Currently I have: model class that talks to the dal class that talks to the connection class I have the following class:
public class Customer : ICustomer
{
private Connection connection;
CustomerDAO db = new CustomerDAO(connection.CreateConnection());
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }

// ctor
public Customer(....)
{
.........
}

public void Create(Customer customer)
{
db.Create(customer)
}
public class Customer : ICustomer
{
private Connection connection;
CustomerDAO db = new CustomerDAO(connection.CreateConnection());
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }

// ctor
public Customer(....)
{
.........
}

public void Create(Customer customer)
{
db.Create(customer)
}
My connection class looks like this
public class Connection
{
private string connectionString = "example string";

public SqlConnection CreateConnection()
{
SqlConnection connection = new SqlConnection(connectionString);
return connection;
}
}
public class Connection
{
private string connectionString = "example string";

public SqlConnection CreateConnection()
{
SqlConnection connection = new SqlConnection(connectionString);
return connection;
}
}
19 Replies
Jimmacle
Jimmacle2y ago
the problem is
CustomerDAO db = new CustomerDAO(connection.CreateConnection());
CustomerDAO db = new CustomerDAO(connection.CreateConnection());
you can't access connection in a field initializer because it's not guaranteed to be initialized at that point. you can assign db in the constructor body instead it looks like the real issue is that the whole Connection class should be static - it doesn't contain any state and it doesn't make sense to ever have an instance of it
kevin
kevinOP2y ago
would i make the entire class static or just the connectionstring ?
Jimmacle
Jimmacle2y ago
the whole class, then you don't need a Connection connection; field you can simply call Connection.CreateConnection() i'm also suspicious why your model needs to contain a database connection, but one thing at a time
kevin
kevinOP2y ago
Currently I get alot of errors in my customerDAO class here's the code
public class CustomerDAO
{
private Connection connection;

public CustomerDAO(Connection connection)
{
this.connection = connection;
}

#region Create
public void Create(Customer customer)
{
using (SqlConnection sqlConnection = connection.CreateConnection())
{
sqlConnection.Open();
string query = "INSERT INTO Customer (Name, Email, Phone, Address) VALUES (@Name, @Email, @Phone, @Address)";
SqlCommand command = new SqlCommand(query, sqlConnection);
command.Parameters.AddWithValue("@Name", customer.Name);
command.Parameters.AddWithValue("@Email", customer.Email);
command.Parameters.AddWithValue("@Phone", customer.Phone);
command.Parameters.AddWithValue("@Address", customer.Address);
command.ExecuteNonQuery();
}
}
#endregion
public class CustomerDAO
{
private Connection connection;

public CustomerDAO(Connection connection)
{
this.connection = connection;
}

#region Create
public void Create(Customer customer)
{
using (SqlConnection sqlConnection = connection.CreateConnection())
{
sqlConnection.Open();
string query = "INSERT INTO Customer (Name, Email, Phone, Address) VALUES (@Name, @Email, @Phone, @Address)";
SqlCommand command = new SqlCommand(query, sqlConnection);
command.Parameters.AddWithValue("@Name", customer.Name);
command.Parameters.AddWithValue("@Email", customer.Email);
command.Parameters.AddWithValue("@Phone", customer.Phone);
command.Parameters.AddWithValue("@Address", customer.Address);
command.ExecuteNonQuery();
}
}
#endregion
kevin
kevinOP2y ago
Jimmacle
Jimmacle2y ago
when you make a class static you have to make all its members static too and you can no longer have variables of that type because there is nothing to store
kevin
kevinOP2y ago
public static class Connection
{
private static string connectionString = "example string";

public static SqlConnection CreateConnection()
{
SqlConnection connection = new SqlConnection(connectionString);
return connection;
}
}
public static class Connection
{
private static string connectionString = "example string";

public static SqlConnection CreateConnection()
{
SqlConnection connection = new SqlConnection(connectionString);
return connection;
}
}
Wait so I would remove the connectionString then like this
public static class Connection
{
public static SqlConnection CreateConnection()
{
SqlConnection connection = new SqlConnection("example string");
return connection;
}
}
public static class Connection
{
public static SqlConnection CreateConnection()
{
SqlConnection connection = new SqlConnection("example string");
return connection;
}
}
Jimmacle
Jimmacle2y ago
no, your first one was fine what errors do you have left?
kevin
kevinOP2y ago
MODiX
MODiX2y ago
Jimmacle
and you can no longer have variables of that type because there is nothing to store
React with ❌ to remove this embed.
kevin
kevinOP2y ago
How would i use the connection now ?
Jimmacle
Jimmacle2y ago
by calling it directly on the type name
kevin
kevinOP2y ago
currently doing it like this using (SqlConnection sqlConnection = connection.CreateConnection())
Jimmacle
Jimmacle2y ago
Connection.CreateConnection() you don't need any connection variables anymore
kevin
kevinOP2y ago
Ah okay, it works now, thanks Wondering though and correct me if im wrong but isnt making it static unsafe because im storing my sql connection string in there ? any object could access the sql connection or am i wrong ?
Jimmacle
Jimmacle2y ago
no, because it's private frankly storing your connection string anywhere in your source code is unsafe, normally you load it from somewhere else like a config file
kevin
kevinOP2y ago
This is for a school project so I’m not really worried about safety that much just wondering Usually i would store it somewhere in .env right ?
Jimmacle
Jimmacle2y ago
environment variables are one option back to this, static on a class just controls whether a class can have instances of itself or not - it doesn't affect accessibility of its members so it's no less safe than it already was the main thing is that it's bad to use static classes for anything that stores information that can change over your program's execution, it tends to make things messy and harder to debug
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?