Hey
i have to make a Database class.
I have a MainForm.cs inside that i have a SettingWindow.cs where i have a some controls such as TextBoxes,RadioButtons,ComboBoxes,
so when the form_load i'm using the Connectionstring from the app.config file but when the user
use the textbox for authentication i just want to test the connection thats it.but the problem im having is that "
The Form_load connection shows database connected but when i am in the settingwindow.cs the database connection is lost.
can you please show where im doing it wrong and if there is a better way doing this ?
thanks
I have so far
public class DAL
{
SqlSetting sqlSetting = new SqlSetting();
string SqlConnectionString = "";
ConnectionState constate;
TextBox tbx_server = new TextBox();
TextBox tbx_dbName = new TextBox();
TextBox tbx_userName =new TextBox();
TextBox tbx_password = new TextBox();
ToolStripStatusLabel tssDbStatus = new ToolStripStatusLabel();//updating status bar
SqlConnection conn;
/// connects to the sql database using the settings from the Application Settings.
public void DBConnectionAppSetting(ToolStripStatusLabel tsDbStatus)
{
this.tssDbStatus = tsDbStatus;
try
{
conn = new SqlConnection(SqlConnectionString);
conn.Open();
constate = ConnectionState.Open;
tssDbStatus.ForeColor = Color.Green;
tssDbStatus.Text = "DB Connected";
}
catch (Exception ex)
{
tssDbStatus.ForeColor = Color.Red;
tssDbStatus.Text = "DB Not Connected";
// Show error message.
MessageBox.Show(ex.Message);
// Stop here
// return;
}
finally
{
// myConnection.Close();
}
}
/// returns the state of the curent sql connection.
public ConnectionState conState()
{
return constate;
}
public void changeUploadGroupBox(string )
{
}
}
}
and i am using it like this
DAL dbCon = new DAL()
dbCon.Conn();
Editors IDEsC#
Last Comment
systan
8/22/2022 - Mon
SAMIR BHOGAYTA
Hi, you have to create .txt file for connection. Then you have to read this file with encrypt and decrypt method. Then after you have to use this file as a dbconnection file.
lalitgada
first of all close the connection before call open connection.
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.