Question declare BindingSource component at run time with sql database

rebelme23

Member
Joined
Mar 23, 2013
Messages
8
Programming Experience
Beginner
I had made a simple app before in which there were two text fields and the BindingSource component was used to fill those fields with data on form load from ms sql server and eventually bind those fields with sql database.

but now what i want is that the user tells the sql file name on run time, and then on next form load the data in the fields should get bind with the sql databse file defined by user.

how to do this???? i thought of so many things... googled many queries but no use.

previously this was the code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load






        'TODO: This line of code loads data into the 'FormdataDataSet.table_form' table. You can move, or remove it, as needed.
        Me.Table_formTableAdapter.Fill(Me.FormdataDataSet.table_form)

where i used these controls:
BindingSource
DataSet
TableAdapter
 

Attachments

  • snap1.jpg
    snap1.jpg
    257 KB · Views: 34
  • snap2.jpg
    snap2.jpg
    269.5 KB · Views: 38
  • snap3.jpg
    snap3.jpg
    287 KB · Views: 33
This has got nothing to do with anything other than your table adapter. First, open you DataSet in the designer, select a table adapter and open the Properties window. I can't recall the exact name but there's a property there that indicates the accessibility of the connection. It will be Private by default and you need to change it to Public. That will add a public Connection property to each table adapter. You can then do something like this:
Dim connection = Me.Table_formTableAdapter.Connection
Dim builder As New SqlConnectionStringBuilder(connection.ConnectionString)

builder.InitialCatalog = databaseName
connection.ConnectionString = builder.ConnectionString
If you need to change the server name or any other property of the connection string then you do that through the connection string builder too.
 
TableAdapter comes in to existence when i declare the datbase source.... pls try to understand-

there are some text fields and the data from sql database should bind to them..
I wan to know how can I declare the DataSource property of BindingSource component at RUN TIME????
 
I think you're missing the point but, to answer your question, just like any other property. Can you add a TextBox and set its Text property at run time? Why would this be any different?
 
VB.NET:
 'Open the mountains file geodatabase feature class[/COLOR]
[COLOR=#0000FF]Dim[/COLOR] mountainsTable [COLOR=#0000FF]As[/COLOR] Table = Table.OpenFileGeodatabaseTable([COLOR=#CC0000]"C:\Data\Scotland.gdb"[/COLOR], [COLOR=#CC0000]"mountains"[/COLOR])

[COLOR=#006633]'Create a new TableBindingAdapter object for the mountains Table[/COLOR]
[COLOR=#0000FF]Dim[/COLOR] tableAdapter [COLOR=#0000FF]As[/COLOR] TableBindingAdapter = [COLOR=#0000FF]New[/COLOR] TableBindingAdapter(mountainsTable)

[COLOR=#006633]'Set the UseCodedValueDomains property to display the descriptive name for the values in any[/COLOR]
[COLOR=#006633]'columns which have a CodedValueDomainDefined[/COLOR]
tableAdapter.UseCodedValueDomains = [COLOR=#0000FF]True[/COLOR]

[COLOR=#006633]'Set the UseColumnAliasNames property to display alias names in the Column headers.[/COLOR]
tableAdapter.UseColumnAliasNames = [COLOR=#0000FF]True[/COLOR]

[COLOR=#006633]'Fill the adapter with all the rows from the mountains Table[/COLOR]
tableAdapter.Fill()

[COLOR=#006633]'Note that the BindingSource component and the DataGridView control would normally be[/COLOR]
[COLOR=#006633]'instantiated by dragging and dropping them onto a Windows Form from the toolbox.[/COLOR]


[COLOR=#006633]'Create a new BindingSource component[/COLOR]
[COLOR=#0000FF]Dim[/COLOR] bindingSource1 [COLOR=#0000FF]As[/COLOR] System.Windows.Forms.BindingSource = [COLOR=#0000FF]New[/COLOR] System.Windows.Forms.BindingSource()
[COLOR=#006633]'Set the DataSource to be the tableAdapter object[/COLOR]
bindingSource1.DataSource = tableAdapter

[COLOR=#006633]'Create a DataGridView control[/COLOR]
[COLOR=#0000FF]Dim[/COLOR] dataGridView1 [COLOR=#0000FF]As[/COLOR] System.Windows.Forms.DataGridView = [COLOR=#0000FF]New[/COLOR] System.Windows.Forms.DataGridView()
[COLOR=#006633]'Set the Datasource to be the bindingSource1 object [/COLOR]
dataGridView1.DataSource = bindingSource1
[CODE]


see this code and i just want the same thing to happen, instead the databse is not gdb but sql. and alos in this code "Table" is giving errors in VS2010
 
I've already told you what to do if you want to specify your own connection string. You can't just pluck code off the ESRI documentation and expect it to work. If you're not prepared to do what I've already told you then I'll just say "have a nice day".
 
thank you for being so rude and not solving the problem... my mistake that I posted the question on this forum. And promise that wont repeat the same. I know I am not pating you for this but as a moderator of this forum and being a developer you shouldn't react that way to people's queries. I am not that good at programming, that's why visited your forum otherwise who wants to waste so much of time for such a small code. Keep your knowledge with you. Thanx.
 
Keep your knowledge with you. Thanx.
If your problem is not solved it's because you chose not to follow the instructions that I provided. If you had done as instructed then there would no longer be a problem. You've been led to water. If you choose not to drink then you'll go thirsty.
 
Back
Top