Я пытаюсь перевести мою строку подключения из одной формы в другую, но она продолжает передавать значение NULL. Я новичок в работе с различными классами, так что это может быть очень простой ошибкой.
Форма 1
public partial class Form1 : Form
{
private string ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Ruben\Documents\dbPlatenCompany.mdf;Integrated Security = True; Connect Timeout = 30";
public Form1()
{
InitializeComponent();
}
public string getConnectionString()
{
return ConnectionString;
}
private void btn_login_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("Select count(*) From tblLogin where Username ='" + txt_username.Text + "' and Password ='" + txt_password.Text + "'", con);
DataTable dt = new DataTable();
sqa.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Form2 main = new Form2();
main.Show();
}
else
{
MessageBox.Show("Username or Password is incorrect");
txt_username.Clear();
txt_password.Clear();
}
}
}
Форма 2
public partial class Form2 : Form
{
private Form1 form1;
public Form2()
{
InitializeComponent();
}
private void btn_search_Click(object sender, EventArgs e)
{
if (rb_Artist.Checked == true)
{
String ConnectionString = form1.getConnectionString();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter sqa = new SqlDataAdapter("SELECT * FROM tblArtist where Name like" + txt_search.Text, con);
DataTable dt = new DataTable();
sqa.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}