Global ConnectionString not being initialized - c#

I have my connection string stored in a myGlobals.cs page as below:
/* Connection String */
public static string conString
{
get { return _conString; }
set { _conString = ConfigurationManager.ConnectionStrings["BaseConnectionString"].ToString(); }
}
I have my code setup in the 4 tier architecture. So I have a Business Object folder, Business Access Layer & Data Access Layer. If I move the connection string into the DAL structure it works fine. Other wise I get this error:
The ConnectionString property has not been initialized
Is the myGlobals.cs class not being included before all this or does it need to be changed around?
Here is my Data Access Layer:
public DataTable Load()
{
SqlConnection conn = new SqlConnection(MyGlobals.conString);
SqlDataAdapter dAd = new SqlDataAdapter("administratorGetAll", conn);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet, "AdministratorsTable");
return dSet.Tables["AdministratorsTable"];
}
catch
{
throw;
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}

The property 'setter' doesn't get called automatically; its code is executed when you put the property on the left-hand side of an assignment - MyClass.MyProperty = "new value";
You want to be doing this:
public static string conString
{
get { return ConfigurationManager.ConnectionStrings["BaseConnectionString"]; }
}

Related

Can't insert the record to database C# SQLite

I've been struggling with an error related to the database. Basically I have an SQLite db and I want to insert the data, but after I execute the method, no data is written to the db but no errors are shown either. This is my code:
The db connection class:
class SqlDb
{
public static SQLiteConnection SqLiteConnection;
public static string DATABASE_WAREHOUSE = "DaneDB.db";
public static string DATABASE_LACZKA =
$"Data Source= {DATABASE_WAREHOUSE};Version=3;New=False;Compress=True;";
public SQLiteConnection Connect()
{
try
{
SqLiteConnection = new SQLiteConnection(DATABASE_LACZKA);
SqLiteConnection.Open();
}
catch (Exception e)
{
MessageBox.Show($"Could not connect to the database {e}");
throw;
}
return SqLiteConnection;
}
public void Disconnect()
{
SqLiteConnection.Close();
}
}
and the inserting method
private void LoadToDb_Click(object sender, EventArgs e)
{
Data modelData = new Data();
SqlDb db = new SqlDb();
SQLiteConnection sqLiteConnection = db.Connect();
SQLiteCommand sqLiteCommand = sqLiteConnection.CreateCommand();
try
{
modelData.Name = firstName.Text;
modelData.Age = Convert.ToInt32(DisplayAge.Text);
modelData.LastName = LastName.Text;
sqLiteCommand.CommandText =
$"insert into DANE values('{modelData.Name}', '{modelData.LastName}', '{modelData.Age}')";
db.Disconnect();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
I also have the options of database set to:
Build Action: Content
Copy to output directory: Copy always
You forgot to execute this command:
sqLiteCommand.ExecuteNonQuery();
Please refer to documentation: ExecuteNonQuery

ConnectionString Value Error

I copied the ConnectString value from the properties of my local database.
Connection String from the properties is :
Data Source=Cyber\SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;
When I directly copy the ConnectionString into ConnectString, I get an error. So I take that "\" off and I did not get an error. However it still does not work. I also noticed that people usually change that ConnectionString value to a single word to make it easy.However, my VB properties section is not allowing me to change it. Here is the error I get
public class SQLConnection
{
#region MemberVariables
private SqlConnection mConnection = null;
private SqlDataAdapter mDataAdapter = null;
private SqlCommand mCommand = null;
static string mDbConnString = string.Empty;
#endregion
#region PublicMemberVariables
public SqlConnection Connection
{
get
{
return mConnection;
}
set
{
mConnection = value;
}
}
public SqlDataAdapter DataAdapter
{
get
{
return mDataAdapter;
}
set
{
mDataAdapter = value;
}
}
public SqlCommand Command
{
get
{
return mCommand;
}
set
{
mCommand = value;
}
}
public string ConnectString
{
get
{
return mDbConnString;
}
set
{
lock (mDbConnString)
{
mDbConnString = value;
}
lock (mConnection)
{
mConnection.ConnectionString = mDbConnString;
}
}
}
#endregion
public void TestConnection()
{
ConnectString = "Data Source=Cyber SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True";
Connection = new SqlConnection(ConnectString);
Connection.Open();
MessageBox.Show(Connection.State.ToString());
}
}
Change
Data Source=Cyber\SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;
To
Data Source=Cyber\\SQLEXPRESS;Initial Catalog=GeneratedData;Integrated Security=True;

Encountering ObjectDisposedException when trying to read from SQLiteDataReader

I am trying to read through a stored SQLiteDataReader object. In theory, it "should" work because the object is stored in a variable before it is referenced (and doesn't hit an error until the reference line is reached), but maybe I have the wrong idea.
I'm trying to keep my application in a neatly layered architecture. So, each database table having its own C# class with its own methods for select, insert, update, and delete; only the data layer knows how to communicate with the database, etc.
I was running into connection issues earlier when I tried to make one static SQLiteConnection object that all the data layer classes could reference (so as to keep it open and minimize overhead, if any). So I'm trying to go with the using block to make sure the connection is properly disposed each time I need to access the database, and hoping that this won't cause performance issues.
So basically, here is the method in my DatabaseConnection class that handles basic query execution:
public SQLiteDataReader ExecuteQuery(string sql)
{
SQLiteDataReader rdr = null;
using(SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
rdr = cmd.ExecuteReader();
}
return rdr;
}
And here is the code that calls that method. I'll use an object/record of the Associate table as an example.
public class Associate
{
public int RowId { get; private set; }
public int Id { get; set; }
public string Name { get; set; }
private string password;
public string Password
{
get
{
return password;
}
set
{
password = Hash(value); // external password hashing method
}
}
public Associate() { } // default constructor with default values
public Associate(int id)
{
this.Id = id;
Select();
}
// select, insert, update, delete methods
private void Select() { ... }
// these are non-queries and return true/false based on success
public bool Insert() { ... }
public bool Update() { ... }
public bool Delete() { ... }
/* Method that causes the error */
public static Associate[] GetAll()
{
DatabaseConnection con = new DatabaseConnection();
SQLiteDataReader rdr = con.ExecuteQuery("SELECT id FROM Associate");
List<Associate> list = new List<Associate>();
if (rdr != null)
{
while (rdr.Read()) /* this line throws the exception */
{
int next = rdr.GetInt32(0);
list.Add(new Associate(next));
}
}
return list.ToArray();
}
}
The idea here is that using the rdr object, I can access the column names directly so that if the database ever changes, I won't have to rewrite a bunch of code to adjust for the column indices (rdr["id"], rdr["name"], etc.)
So what I don't understand is why rdr in the calling method is having "object disposed" issues because it's stored in a variable before I reference it. I know the connection is disposed at the end of the called method, but since the returned result is stored, shouldn't it technically be able to "survive" outside the using block?
It is the connection that got disposed. The data reader can only read data while the connection still exists.
public SQLiteDataReader ExecuteQuery(string sql)
{
SQLiteDataReader rdr = null;
using(SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
rdr = cmd.ExecuteReader();
}
// *** Connection gone at this stage ***
return rdr;
}
Your options are to either return a DataTable, e.g.
public DataTable ExecuteQuery(string sql)
{
SQLiteDataReader rdr = null;
using(SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
conn.Open();
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
rdr = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(rdr);
return dataTable;
}
}
otherwise, you could keep the connection alive inside the DatabaseConnection class:
class DatabaseConnection : IDisposable
{
private readonly IDbConnection _conn;
public DatabaseConnection()
{
_conn = new SQLiteConnection(ConnectionString);
}
public void Dispose()
{
_conn.Dispose();
}
public SQLDataReader ExecuteQuery(string sql)
{
...
}
}
// sample usage
using (var conn = new DatabaseConnection())
{
using (var reader = conn.ExecuteQuery("SELECT ...")
{
// do your work in here
}
}

Cannot implicitly convert type 'void' to 'string'

I've tried for a while now and I really dont get it. I recive error "Cannot implicitly convert type 'void' to 'string'"
I have tried multiple variants of string, int, nothing, void, public, static and nope I really dont get it right.
I want to get one value from my db thoug my DAL and BLL, my code looks like this.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = BLL.getGames();
Repeater1.DataBind();
var culture = CultureInfo.GetCultureInfo("sv-SE");
var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);
var dateTime = DateTime.Today;
int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
string mroundY = dateTime.Year.ToString();
string mroundW = weekNumber.ToString();
string mrounddate = mroundY + mroundW;
string mround = BLL.getMroundGames(mrounddate); <-- Here's the error
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
}
}
My BLL looks like this;
public class BLL
{
public static void getMroundGames(string mrounddate)
{
SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = #mrounddate");
DAL.ExecuteNonQuery(getMroundGames);
}
}
Also tried this;
public class BLL
{
public static DataTable getMroundGames(string mrounddate)
{
SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = #mrounddate");
getMroundGames.Parameters.Add("#mrounddate", SqlDbType.VarChar, 10).Value = mrounddate;
return DAL.GetData(getMroundGames);
}
}
And finaly my DAL looks like this;
public class DAL
{
public static SqlConnection GetConnection()
{
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["tiptopConnectionString"].ConnectionString);
conn.Open();
return conn;
}
public static DataTable GetData(SqlCommand command)
{
try
{
using (SqlConnection conn = GetConnection())
{
using (DataSet ds = new DataSet())
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = command;
da.SelectCommand.Connection = conn;
da.Fill(ds);
return ds.Tables[0];
}
}
}
}
catch (Exception err)
{
throw new ApplicationException(string.Format("Felmeddelande:{0}", err.Message));
}
}
public static object ExecuteScalar(SqlCommand command)
{
using (SqlConnection conn = GetConnection())
{
command.Connection = conn;
object result = command.ExecuteScalar();
return result;
}
}
public static void ExecuteNonQuery(SqlCommand command)
{
using (SqlConnection conn = GetConnection())
{
command.Connection = conn;
command.ExecuteNonQuery();
}
}
}
Where to begin?
The signature for this is wrong;
public static void getMroundGames(string mrounddate)
You need to change it to something similar to ;
public static string getMroundGames(string mrounddate)
Retrieve the string value from your DAL and return to the consumer accordingly.
var dt = Dal.GetData();
return (string)dt.Rows[0]["field"];
However, in all honesty, I would not be passing a datatable from your DAL to your BLL. I would return the string directly or introduce a DTO and populate this from your DAL through your BLL, back to the consumer.
You need to add a return type of string to getMroundGameas(string mrounddate).
It's not clear what type of object DAL is, but you should also probably use ExecuteReader method, http://msdn.microsoft.com/en-us/library/bb344397.aspx
rather then ExecuteNonQuery. This will return a Reader which can be queried for the value returned by the select statement.
Finally you should return the value.

Cannot connect to SQL database - loss of username and password

having difficulty with a bit of code I have. I think variables are stuck inside the method I'm invoking and when I go to run that method again variables are no longer there.
Below is an example of my main:
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
sqlconnect sql = new sqlconnect();
public string pass;
public string user;
public void button1_Click(object sender, EventArgs e)
{
//Username and password textboxes send to public strings
user = textBox1.Text;
pass = textBox2.Text;
try
{
//try connecting to SQL database using SQL class
sql.GetSqlConnection();
sql.myConnection.Open();
this.Hide();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Here is the SQL connection class:
namespace NHS_Workshop_System
{
public class sqlconnect
{
public SqlConnection myConnection { get; set; }
Settings1 set = new Settings1();
Login var = new Login();
public SqlConnection GetSqlConnection()
{
if (myConnection == null)
myConnection = new SqlConnection("user id="+(var.user)+"; password="+(var.pass)+";server="+(set.SelectServer)+";Trusted_Connection="+(set.SelectContype)+";database="+(set.SelectDataBase)+"; connection timeout="+(set.Condrop)+"");
return myConnection;
}
}
}
So when I try to login it may work first time but if another form tries to run that method the username and password are missing. I'm not sure how to make the variables global so that every time I run the method it calls those details without fail. So the second time it uses sqlconnection on another form the ex message states that username and password are non existent. Is this a scoping issue if so can someone shed some light on how i might go about this maybe a new method of gaining access to an SQL server? Thank you tearing out my hair
Below is the answer for your question the way you have it now. But I strongly recommend you to think about what you are doing since your class logics doesn't seem to be very good. I see that your trying to implement Singleton which is good, but you can still make more instances of sqlconnect if you would like.
In your Login class, pass this instance of Login so the sqlconnector knows the user and pass that is declared here.
public partial class Login : Form
{
// Old style - sqlconnect sql = new sqlconnect();
sqlconnect sql;
public string pass; // These arent declared and wont be of use for your sql connect class
public string user;
public Login( String pass, String user )
{
this.pass = pass;
this.user = user;
// Now that we have a login with declared variables, pass it to the sqlconnect object
this.sql = new sqlconnect(this);
InitializeComponent();
}
Now in your sqlconnect class, before your Login variable was just empty. Now you have received an instance of it in your constructor that has it's variables set,
public class sqlconnect
{
public SqlConnection myConnection { get; set; }
Settings1 set = new Settings1();
// Here you maked a new one, use the existing instead from which this class is called.
// changed variable named from var to login..
Login login;
// *NEW* Constructor where you pass your login object
public sqlconnect(Login login)
{
this.login = login;
}
public SqlConnection GetSqlConnection()
{
if (myConnection == null)
myConnection = new SqlConnection("user id="+(login.user)+"; password="+(login.pass)+";server="+(set.SelectServer)+";Trusted_Connection="+(set.SelectContype)+";database="+(set.SelectDataBase)+"; connection timeout="+(set.Condrop)+"");
return myConnection;
}
}

Categories

Resources