[RESOLVED] SQL Server connection string from other Form
Hi how can I pass the connection string from my Main Form to Another form without retyping again those strings. Let say I have separate form for Adding data to database.
Here is my connection codes.
Code:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
SqlConnection con;
DataSet ds1;
SqlDataAdapter da;
private void frmMain_Load(object sender, EventArgs e)
{
}
private void loadRecords_Click(object sender, EventArgs e)
{
con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyMDB Records\\myMDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
ds1 = new DataSet();
con.Open();
string sql = "SELECT * FROM tblClients";
da = new SqlDataAdapter(sql, con);
da.Fill(ds1, "Clients");
loadLitview();
con.Close();
con.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
frmAdd FormAdd = new frmAdd();
FormAdd.Show();
}
Ho can I make that call from my other form which is Adding records to db?
Re: SQL Server connection string from other Form
Code:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
public static SqlConnection con;
DataSet ds1;
SqlDataAdapter da;
public static function makeConnection()
{
if(frmMain.con != null)
{
frmMain.con = new SqlConnection();
frmMain.con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyMDB Records\\myMDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
}
}
private void frmMain_Load(object sender, EventArgs e)
{
}
private void loadRecords_Click(object sender, EventArgs e)
{
frmMain.makeConnection();
ds1 = new DataSet();
frmMain.con.Open();
string sql = "SELECT * FROM tblClients";
da = new SqlDataAdapter(sql, con);
da.Fill(ds1, "Clients");
loadLitview();
frmMain.con.Close();
frmMain.con.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
frmAdd FormAdd = new frmAdd();
FormAdd.Show();
}
Should do the trick.
Re: SQL Server connection string from other Form
I am getting error here
Quote:
Error 1 : The type or namespace name 'function' could not be found (are you missing a using directive or an assembly reference?
Wich point here
Quote:
public static function makeConnection()
Do I missed namespace?
Re: SQL Server connection string from other Form
Change it to void.
Code:
public static void makeConnection()
{
if(frmMain.con != null)
{
frmMain.con = new SqlConnection();
frmMain.con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyMDB Records\\myMDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
}
}
Re: SQL Server connection string from other Form
I changed to void
But still getting error
This time here
Code:
frmMain.con.Open();
Unhandled exemption
Anyways thanks for the help. maybe I just retype the strings into another form. Thanks.
Re: SQL Server connection string from other Form
Why not just put it in a config file?
Re: SQL Server connection string from other Form
As dee-u says, just put it in the config file and then you don't have to pass it anywhere. Open the Settings page of the project properties and add a new setting of type ConnectionString. You can then access it anywhere in code using Properties.Settings.Default.MyConnectionString, where MyConnectionString is the name you give the setting.
Re: SQL Server connection string from other Form
Quote:
Originally Posted by
dee-u
Change it to void.
heheh.... :blush:
Too much PHP lately
Re: SQL Server connection string from other Form
Quote:
As dee-u says, just put it in the config file and then you don't have to pass it anywhere. Open the Settings page of the project properties and add a new setting of type ConnectionString. You can then access it anywhere in code using Properties.Settings.Default.MyConnectionString, where MyConnectionString is the name you give the setting.
__________________
i think the the attached way ?let me know jmcilhinney.if i am wrong ?:D:D:D
Re: SQL Server connection string from other Form
Quote:
Originally Posted by
firoz.raj
__________________
i think the the attached way ?let me know jmcilhinney.if i am wrong ?:D:D:D
I said create a setting of type ConnectionString. That's not the type you have selected.
Re: SQL Server connection string from other Form
Thanks for the idea. Maybe I will recode my application :D
Re: [RESOLVED] SQL Server connection string from other Form
Quote:
I said create a setting of type ConnectionString. That's not the type you have selected.
but still i am getting Duplication Issue .Kindly find the attachment also.
Code:
Error 1 The item "obj\Release\MakeConnection.FrmArtist.resources" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter. MakeConnection