Results 1 to 12 of 12

Thread: [RESOLVED] SQL Server connection string from other Form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [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.....

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    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.....

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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";
                }
            }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    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.....

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL Server connection string from other Form

    Why not just put it in a config file?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: SQL Server connection string from other Form

    Quote Originally Posted by dee-u View Post
    Change it to void.
    heheh....
    Too much PHP lately
    Delete it. They just clutter threads anyway.

  9. #9
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question 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.
    __________________
    i think the the attached way ?let me know jmcilhinney.if i am wrong ?
    Last edited by firoz.raj; Sep 20th, 2010 at 03:28 AM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: SQL Server connection string from other Form

    Quote Originally Posted by firoz.raj View Post
    __________________
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    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.....

  12. #12
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question 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
  •  



Click Here to Expand Forum to Full Width