Grade my connection logic..

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

47 Replies - 1707 Views - Last Post: 30 September 2013 - 02:42 PM Rate Topic: -----

#31 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 12:51 PM

View PostCurtis Rutland, on 19 September 2013 - 02:42 PM, said:

You also don't have to "hide" the column if you don't use auto-generated columns. If you define your own, you can just skip the ID columns (And any others you don't want to display), but the data will still be there for you when you get the SelectedValue.


Holy crap.. longest class ever! I've never seen so many properties and methods.. that's a little intimidating.

All of my tables have PKs, but I did auto generate because that's what we always did in school. How would I set the PK myself? I mean.. I know I can use any column I want for the PK. But To always ensure that it's unique, I would have to have the user put in something every time they added a device? There must be something else you're talking about, because that is NOT a good idea LOL.
Was This Post Helpful? 0
  • +
  • -

#32 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 01:33 PM

Quote

All of my tables have PKs, but I did auto generate because that's what we always did in school.


Strangely enough, that's one case where school got it right. You should use auto-number columns as PKs unless (again) you have a good reason not to. That, or a GUID column, but that's not worth going into detail on right now.

Basically, you can pretty much guarantee that an auto-number is unique. Any other columns coming from your business requirements could change in the future to be non-unique.

Quote

Holy crap.. longest class ever


Yeah, you're going to see that a lot on GUI Controls. Don't worry about memorizing them or even knowing what everything does, you won't use most of it except for special cases. Just check out the sample code at the bottom and things'll be a lot clearer.
Was This Post Helpful? 0
  • +
  • -

#33 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 01:41 PM

Okay. Demo is purring like a slightly mangled kitten, and ready to go for the boss tomorrow.

Now, onto the good stuff.

So, I want to include the DeviceID in the dataset as a unique identifier. But I don't want to show it in the datagrid because it's not relevant to the user.

So. is this the proper way to do that?

 
if (e.Column.Header.ToString() == "DID")
e.Cancel = true;



Found it in the properties. The actual data would still be available, because it's in the dataset, correct?

EDIT.. and I thought this would work, It's throwing an invalid cast error (

int devID = (int)row["DID"];


Double checked the DB, the field is indeed an int. I checked the DataRowViewClass (albeit briefly) and it seems like it can take either an int or a string as an arg.

EDIT:: I got this working in about 3 minutes this morning LOL. It's amazing what stepping away and clearing your head will do for your thought processes.

If anyone reads this thread for help one day: you can't cast a DataRowView field to an int. You have to cast it to a string first, then convert it to an int.

This post has been edited by synlight: 20 September 2013 - 05:26 AM

Was This Post Helpful? 0
  • +
  • -

#34 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 20 September 2013 - 06:48 AM

What is the most appropriate way to backup your project. I just realized that if something happened. I would be hosed. I did some searching, and it seems like my choices are to make a copy of the solution folder (not recommended.. why?), upload it to git, or use Source Control (currently reading about that).

What do you guys recommend?

Also, should I start a new thread for this? I just realized I probably should. But I don't want to clutter up page 1 with silly questions.
Was This Post Helpful? 0
  • +
  • -

#35 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 20 September 2013 - 07:07 AM

Quote

you can't cast a DataRowView field to an int. You have to cast it to a string first, then convert it to an int.


That actually depends on the type of data in the field. It's only a string if the source column was a varchar (or whatever Sqlite calls a string). If the column is defined as an integer on the database, you should be able to cast it directly as an int. Of course, I've never worked with Sqlite, so I'm not sure if it does things differently.

As to backup...well, you mentioned three options. Copying a folder, Git, and Source Control. First thing: git is a type of source control. Source Control is kind of like a category of things that all do the same job. There's lots of free options (CVS, SVN, Git, Mercurial, etc...) and there's some that aren't always free (VSS, TFS, plenty of others I've never used).

My recommendation is Git. If you want what everyone else uses, Github is great. But if you want a private repository, you have to pay them, otherwise your source is visible to all (which is great for open source projects, not so good for work done for a company).

BitBucket offers free private git repos.

But if you're working for a company, it's better to set up some kind of source control server on your network. Maybe a Team Foundation Server (if you guys are paying for an MSDN subscription, might as well use it). If you want to go free, you can install your own Git server.

Trust me, there's a lot to learn with Git and other source control solutions. Just start with getting your code checked in, and worry about all the branching and forking and merging and cloning and everything else later.

As to why you don't just copy the folder as a backup solution...the idea of source control is versioning. If you wanted that with the folder-copy method, every time you wanted to "check in", you'd have to make a new copy, and keep all your copies labeled, so you can go back to the exact change you want to undo. With source control, that's all handled for you, so you just do things like committing or checking in.

Also, a source control server usually isn't located on the same machine you're programming on. What if your HDD dies? Then all your code is gone. That's why we suggest source control that is backed up by a backup solution.
Was This Post Helpful? 1
  • +
  • -

#36 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 20 September 2013 - 07:12 AM

Hmm,.. that's very strange. The field I was trying to use is an int in the database, I triple checked. It's auto generated.. I wonder if that has anything to do with it? I couldn't get it to work, I tried several times. I finally had to give up and cast it into a String, then back into an int. Very odd.

I do have a BitBucket repository now LOL, I'm going through the tutorial right now holy cow. I did also make a quick copy and put it on an external HDD until I get the Git thing figured out.
Was This Post Helpful? 0
  • +
  • -

#37 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 20 September 2013 - 07:17 AM

Well, it could be the way that the Sqlite components work; they might behave differently than the built in ones for Sql Server. Use what works.
Was This Post Helpful? 0
  • +
  • -

#38 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 20 September 2013 - 08:24 AM

Okay I have 4 hours today to play with my program and try to clean it up a bit. I went back over page 1 of the thread, I'm doing baby steps so I don't get overwhelmed.

I want to incorporate the using statements like Michael suggested. That won't be difficult, just a little time consuming.

Then.. I want to start centralizing my database functionality. To start, I have 3 questions:

1. Is it a bad idea to instantiate an instance of the database class, leave it open the whole time the program is running, then close it when the software is shutting down? Curtis kind of showed it that way in one of his posts. Would I do that in the App.xaml file?


2. If I have 2 tables, one with 20 records, and one with up to 200 records, will it slow down performance to load them both into persistent datasets, then destroy the data sets when the program shuts down? Or should I fill datasets as I need them?

3. My file tree is driving me insane. I want to organize it but I honestly am afraid I'll start breaking stuff by creating folders and putting things in them. I do intend to completely change my way of doing things into MVVM or something like it, but that is not something I can get done today. Especially since my next priority is figuring out how to separate my database stuff from my windows.

I know that's a lot of questions. I figured it would be best to get them all out now, instead of posting a million times.
Was This Post Helpful? 0
  • +
  • -

#39 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 20 September 2013 - 08:43 AM

You have some options here. Notice that I didn't leave any connections open though, because the using statements call Dispose when they're done. Dispose does whatever the original developer made it do, but that's supposed to clean up after itself. And I know from experience that classes that derive from DbConnection will close any active connections when they're disposed.

So really, the code I showed opens and closes the connection each time you need to query, which is actually the correct way to go about this. What is staying alive is an instance of the DataManager class, which doesn't really matter, because you can see it creates all the connection objects inside the method, so when the method is done, the GC can get rid of them all, and your class isn't taking up a bunch of resources or memory.

Quote

If I have 2 tables, one with 20 records, and one with up to 200 records, will it slow down performance to load them both into persistent datasets, then destroy the data sets when the program shuts down? Or should I fill datasets as I need them?


Unless you have huge images or binaries in those rows (I'm sure you don't), that's a miniscule amount of data. I wouldn't worry too much. Some of the databases on the last job I worked on had hundreds of gigabytes of data in them, and that's still not all that large when you compare it to "Big Data" stuff. Do whatever is easier for you in this case, because that's not enough data to matter about "the right way".

Quote

My file tree is driving me insane. I want to organize it but I honestly am afraid I'll start breaking stuff by creating folders and putting things in them. I do intend to completely change my way of doing things into MVVM or something like it, but that is not something I can get done today. Especially since my next priority is figuring out how to separate my database stuff from my windows.


Folder structure is arbitrary. What matters to your project is namespaces. That's how it knows where to find code. Usually we like to keep our namespaces the same as the structure of our folders (so if I had a base namespace of "Com.CurtisRutland.MyProject" and I had a folder called "Utilities", I prefer the files in there to have the namespace of "Com.CurtisRutland.MyProject.Utilities"), but it's not a hard and fast rule.

I'd suggest re-organizing one piece at a time. Then compile after each move, so you can track down fewer problems at a time. In fact, compile all the time. It saves your files and points out errors. I use it all the time to check my work or to make it easier to jump to an error I know I created with my changes.
Was This Post Helpful? 1
  • +
  • -

#40 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 23 September 2013 - 12:23 PM

Sigh. I'm starting to regret my decision to go with SQLite. Maybe i should have stuck with MSSQL. Google is laughing at my searches for stuff like 'persistent datasets in SQLite'.

Okay, truthfully I'm really frustrated LOL. Although I am thoroughly enjoying the XAMLStyler Thln 'Toq (I know I mangle your name, I'm sorry) posted in his WPF tutorial.

Maybe I need to take a break, I worked through lunch again. In Visual Studio, I found the add Dataset tool.. but since I'm using SQLite, I can't really use it because I'm not using any of the supported data connections. Or maybe my Googler is broken. Maybe I should have stayed with MSSQL because tutorials are everywhere, and I'm having a hard time making MSSQL == SQLite in translation. I have no real question here, so.. idk what I even want to accomplish. Somebody to talk to, I guess.
Was This Post Helpful? 0
  • +
  • -

#41 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 23 September 2013 - 12:32 PM

Well, one good thing is that it shouldn't be too hard to yank out your Sqlite stuff and replace it with MS SQL CE. Especially once you refactor everything so you don't do database stuff outside of your DataManager class. The components are all built from the same base classes (DbDataReader, DbConnection, DbCommand, etc) from the System.Data.Common namespace, so for the most part, you can swap them for the ones that match the database you want to use and you don't have to re-work your logic.

You can also look into Enterprise Library, which makes it very easy to swap databases (the idea is you write generic DB code, and use the web/app.config to actually configure your DB Type and Connection Strings). But it's a big, big library and probably a bit much for now.

You can also look into Entity Framework. It can read your DB's schema and create models for you, so you don't have to deal with DataSets, you can just deal with objects that match your DB schema perfectly. You have to learn about LINQ to use it, but it's worth trying:

http://www.codeproje...-entity-framewo

I'd suggest creating a simple separate application to learn, then trying to apply what you've learned to your main project.
Was This Post Helpful? 1
  • +
  • -

#42 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 23 September 2013 - 12:36 PM

I was reading in the advanced forum earlier about local databases. I wonder if that would be a good option for me, since there's not GB of data to worry about? Seems like it would make my learning curve a lot less steep. Of course part of me feels like I would be copping out to walk away from the original decision at this point too.

ETA: I agree about the Enterprise library, someone suggested it in anther of my threads a couple of weeks ago, but I don't think I'm quite ready for that. I did some reading on the EF this morning.. at this point I'm doing more reading than coding which is great for my knowledge base, but not so great for the boss.

This post has been edited by synlight: 23 September 2013 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#43 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 23 September 2013 - 12:39 PM

"local databases" refers to MS Sql Server Compact Edition (MSSQLCE). It's an embedded DB like Sqlite, and it's a pretty good choice for .NET apps like this one.

Also, I think I was editing my post when you posted, so there's probably more there than you saw.

My preference for this kind of thing is SQLCE + Entity Framework. But I'm fairly familiar with LINQ, which is pretty important to use EF.
Was This Post Helpful? 0
  • +
  • -

#44 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 84
  • View blog
  • Posts: 557
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 23 September 2013 - 12:43 PM

I'm going to take a break. I've been at my desk for 7 hours, I have a hard time stepping away when I feel like there's so much work to be done.

So.. if I switch to the local DB, I will be able to use all of the functionality Visual Studio supplies for simple DB connections, datasets, etc,, more easily? And by easily, I mean 'at all'? :helpsmilie:/>/>

Edit: My current database does everything it *needs* to do. It does work. I just want to apply a lot of the concepts discussed in this thread, and I'm having a hard time doing that with my current setup.

Edit again: I guess I can't use the local option, my files are on a network share, and it's not supported by MSSQLCE.

So I guess the next step would be SQLServer Express. I do have it installed on my machine, but I need to research how difficult it would be to deploy with my application.

This post has been edited by synlight: 23 September 2013 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#45 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,540
  • Joined: 12-December 12

Re: Grade my connection logic..

Posted 23 September 2013 - 03:04 PM

To help get you started ;)

Deploying SQL Server Express :technet

There are further links and information at this SO thread.
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4