4 Replies - 570 Views - Last Post: 06 December 2012 - 12:56 PM Rate Topic: -----

#1 Darkmatter5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 10

DbConnectionBuilder and class question

Posted 06 December 2012 - 12:07 PM

Here is my code:

class dbLocalDB : DataManager
{
  private System.Data.Common.DbConnectionStringBuilder BldrLocalDB = new System.Data.Common.DbConnectionStringBuilder();

  public System.Data.Common.DbConnectionStringBuilder ConnectionString
  {
    get { return BldrLocalDB; }
    set
    {
      BldrLocalDB.ConnectionString = XenoManager.Properties.Settings.Default.csLocalDB;
    }
  }
}



public partial class Form1 : Form
{
  dbLocalDB LocalDB = new dbLocalDB();

  public Form1()
  {
    InitializeComponent();

    //Populate textboxes
    try
    {
      tb_LocalDBServerURL.Text = LocalDB.ConnectionString["Data Source"].ToString();
      tb_LocalDBDatabase.Text = LocalDB.ConnectionString["Initial Catalog"].ToString();
      tb_LocalDBUsername.Text = LocalDB.ConnectionString["User ID"].ToString();
      tb_LocalDBPassword.Text = LocalDB.ConnectionString["Password"].ToString();
    }
    catch (Exception ex)
    {
      textBox1.Text = ex.ToString();
    }
  }
}



I get the following error "System.ArgumentException: Keyword not supported: 'Data Source'. at System.Data.Common.DbConnectionStringBuilder.get_Item(String keyword)"

I think what's happening is BldrLocalDB.ConnectionString isn't being populated. If I had:

System.Data.Common.DbConnectionStringBuilder bldrLocalDB = new System.Data.Common.DbConnectionStringBuilder();
bldrLocalDB.ConnectionString = Properties.Settings.Default.csLocalDB;



in "public partial class Form1 : Form", it'll populate, but how do I populate it in the dbLocalDB class?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: DbConnectionBuilder and class question

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: DbConnectionBuilder and class question

Posted 06 December 2012 - 12:32 PM

The set method isn't automatically called so that code never runs. Put a constructor in there and set it, or 'set' the value (which will ignore the value you pass and set it to the Settings value.

Edit: The second way I mention is a bad way to do it, it will confuse you or anyone who later looks at this code as to what is really going on. Use the constructor and remove the set method entirely.

This post has been edited by Momerath: 06 December 2012 - 12:34 PM

Was This Post Helpful? 1
  • +
  • -

#3 Darkmatter5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 10

Re: DbConnectionBuilder and class question

Posted 06 December 2012 - 12:43 PM

Thank you for the reply, but I'm still trying to learn these concepts. Can you post some example code?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: DbConnectionBuilder and class question

Posted 06 December 2012 - 12:47 PM

using System.Data.Common;

class dbLocalDB : DataManager {
    private DbConnectionStringBuilder BldrLocalDB = new DbConnectionStringBuilder();

    public DbConnectionStringBuilder ConnectionString {
        get { return BldrLocalDB; }
    }

    public dbLocalDB() {    // Parameter-less constructor
        BldrLocalDB = XenoManager.Properties.Settings.Default.csLocalDB;
    }
}

Was This Post Helpful? 1
  • +
  • -

#5 Darkmatter5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 10

Re: DbConnectionBuilder and class question

Posted 06 December 2012 - 12:56 PM

Perfect it worked, however it had to be "BldrLocalDB.ConnectionString = XenoManager.Properties.Settings.Default.csLocalDB;"

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1