|
-
Sep 17th, 2010, 01:50 PM
#1
Thread Starter
Hyperactive Member
[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?
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Sep 17th, 2010, 04:22 PM
#2
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.
Last edited by TheBigB; Sep 17th, 2010 at 04:28 PM.
Delete it. They just clutter threads anyway.
-
Sep 17th, 2010, 06:41 PM
#3
Thread Starter
Hyperactive Member
Re: SQL Server connection string from other Form
I am getting error here
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
public static function makeConnection()
Do I missed namespace?
Last edited by dr_aybyd; Sep 17th, 2010 at 06:50 PM.
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Sep 17th, 2010, 09:14 PM
#4
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";
}
}
-
Sep 17th, 2010, 10:35 PM
#5
Thread Starter
Hyperactive Member
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.
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Sep 17th, 2010, 10:55 PM
#6
Re: SQL Server connection string from other Form
Why not just put it in a config file?
-
Sep 18th, 2010, 06:21 AM
#7
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.
-
Sep 18th, 2010, 07:41 AM
#8
Re: SQL Server connection string from other Form
 Originally Posted by dee-u
Change it to void.
heheh.... 
Too much PHP lately
Delete it. They just clutter threads anyway.
-
Sep 19th, 2010, 12:23 AM
#9
Frenzied Member
Last edited by firoz.raj; Sep 20th, 2010 at 03:28 AM.
-
Sep 19th, 2010, 01:02 AM
#10
Re: SQL Server connection string from other Form
 Originally Posted by firoz.raj
__________________
i think the the attached way ?let me know jmcilhinney.if i am wrong ?   
I said create a setting of type ConnectionString. That's not the type you have selected.
-
Sep 19th, 2010, 01:20 AM
#11
Thread Starter
Hyperactive Member
Re: SQL Server connection string from other Form
Thanks for the idea. Maybe I will recode my application
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Sep 19th, 2010, 03:20 AM
#12
Frenzied Member
Re: [RESOLVED] SQL Server connection string from other Form
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
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|