Link to home
Start Free TrialLog in
Avatar of jdraggi
jdraggiFlag for United States of America

asked on

I need a super simple way to insert a datarow into sql using vb.net, the datarow has same scheme.

I need a super simple way to insert a datarow into sql using vb.net, the datarow has same scheme.

It seems simple but my searches have turned up methods that require me to parameter everything.  Isn't there a simple function that someone's already written to insert a datarow?  Maybe I need to change the process which I am reading the data as well.

Thanks for your help,
--john
ASKER CERTIFIED SOLUTION
Avatar of Malik1947
Malik1947

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdraggi

ASKER

That's pretty cool stuff.  I noticed that everything that they are doing revolves around a dataset or datatable.  Which is fine however a lot of my data involves comboboxes and textboxes.  How would you suggest by this method that I interface a textbox or combobox?

Lets say the project needs to call a SQL database, take column1 into textbox1 and column2 into combobox2.  Then after I make changes to either of these I need to click a button to push the changes back to SQL.

My issue here is specifically how do I link the textbox or combobox to that datarow item?


Thanks,
John
Avatar of Malik1947
Malik1947

one of the tutorials... i think it is the third one shows how to use text boxes and combo boxes. they can enter data into the fields and then you click save then it enters the information into the database.
Avatar of jdraggi

ASKER

Yes, I've figured out how to do that stuff... I am currently trying to figure out how to have my combo boxes show the options from a codes section of the database and have the selected item be the option that the database currently has.

Any ideas?

--john
Avatar of jdraggi

ASKER

Currently what I ended up doing was adding a database dataset separate from the LINQ dataobject and setting the combobox to pickup the displaymember and value through the dataset choosing the selected value item that the LINQ dataobject has.

What I'm looking for now is clarity that this is the best way to handle this.  Because this method is new to me I am unlcear if this is the correct method or if I should be using the LINQ data object.

It would appear that if I use LINQ that I would be adding more work to my project because I would then have to create separate select statements for each coded box.

Thanks,
John
I think what you are doing is correct. That's how I would have done it.
Avatar of jdraggi

ASKER

I was working with it and kept trying different combinations...  it's kind of freaky...

What I ended up doing was bringing in a clients database into the LINQ server explorer, then brought in the associated tables.  In my database I already setup the relationship so the field mapping didn't have to be re-mapped.

ok, so now I have a LINQ datasource... here's where it gets cool...  In your datasource explorer expand your database and you'll see your relational datasources.  Right click in the data sources explorer (anywhere) and choose Add new datasource...  you can now drill down and add your codes relationship database...

The tricky part is this...  Normally if you had just been using a text box or a datagridview you would just drag and drop from the original client database source...  in this case, if you want to add a combobox that is going to have the relational codes you have to drill into your codes datasource, you'll see the original database source + (s) so client datasource will now be clients.  ok, so now you will see the same tables and you can change the relational one to a combobox and drag it over to your form.

Now, the second part of this is that you have to click the properties ">" on the combobox and set the data source to your codes data, this gives you the options needed to set the display member and the value member... then you also need to set the selected value member to your original client datasource and it's corresponding field.

Once you do it a couple times it makes sense ...  After I posted I got thinking about what I said because although that method works one of the other videos from your link talked about conversions from datasets and LINQ will still work with your existing application...  so although it does work the preference is to move away from that older method.

The code behind is really sweet too...

Define the database
Dim ClientDB As New MYdbDataContext()

in the load section load up your data:

'Use this to review the sql statements that are actually being sent
ClientDB.Log = Console.Out

'Load up your client
Dim ThisClient = From Client In ClientDB.Clients Where Client.ClientGUID = GlobalClientGUID Select Client
'Assign the client to a datasource that will be update/etc later
ClientBindingSource.DataSource = ThisClient

'Load up your Codes
Dim CD_DOBQuality = From code_dobquality In ClientDB.Code_DOBQualities Select code_dobquality
'Assign the codes to the datasource that the combobox is pulling from
CodeDOBQualityBindingSource.DataSource = CD_DOBQuality

When you're all done and need to save changes...
Me.ClientBindingSource.EndEdit()
ClientDB.SubmitChanges()

So, it's a bit tricky at first but once you get it... it's cake.

The only problem that I'm having now is updating a field via code.  I'm using GUIDs to track everything and can not figure out how to assign the GUID through the software....  IE if I'm not using a textbox/combobox/etc...  This is driving me nuts because it should be the most simple part of this.  LOL

Any ideas?

--John

Avatar of jdraggi

ASKER

OK, figured it out....

Dim NewClient = New Client With {.ClientGUID = GlobalClientGUID}
ClientBindingSource.Add(NewClient)

That was a lot of work to learn about LINQ to SQL but this will save me so much time that it's definitely worth it.

Thanks,
--John
Avatar of jdraggi

ASKER

I wanted to add that we found that the intellisense isn't working in some parts as well...

Dim test As Client = ClientBindingSource.Current
test.FirstName = "John"

is the same as:
ClientBindingSource.Current.FirstName = "John"

However the intellisense doesn't give you the table field names in the latter whereas the Dim'n it out does.

--john