Grade my connection logic..

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

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

#16 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 06:35 AM

View PostMichael26, on 19 September 2013 - 05:59 AM, said:

...
Is that intended to be that way?...


MrShoes did a good job explaining this, it's just like nested loops without brackets. The only difference here is that VS will automatically format the code so nested usings don't tab in. Once you get used to reading it, it makes sense. The idea is that technically each using starts its own scope, but if you are only going to put code in the inner-most one, effectively there's only one real scope. So you can just stack them up and pretend like they're all on the same line.

@synlight, don't get discouraged, like I said in my other post, it takes years and practice to put it all together. The ORM MrShoes mentioned is the same thing I linked earlier. It's worth a learn, because it can save you a lot of time, but I'd probably suggest not using it for this project, since you've already made progress. Consider one next time.
Was This Post Helpful? 3
  • +
  • -

#17 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 351
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: Grade my connection logic..

Posted 19 September 2013 - 06:54 AM

So they are like for loop with only one line after?
Was This Post Helpful? 0
  • +
  • -

#18 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 07:02 AM

Basically. Think of it like this:

for(var i = 0; i < 10; i++)
    for(var j = 0; j < 10; j++)
    {
        Console.WriteLine(i);
        Console.WriteLine(j);
    }



That's completely valid too. Since the inner loop is a block statement, you don't have to wrap it in curly braces. The only difference is that VS will tab in the for loops, but it will keep the usings on the same indention.
Was This Post Helpful? 0
  • +
  • -

#19 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 08:14 AM

This question isn't about connection logic.. but it's kind of in the same vein.

I have a main console, which is the hub for users to choose where they want to go. Up until now I was destroying the window when a user opened another one, and instantiating a new window when they indicated they wanted to go back.

That sounds like the wrong thing to do given my research today. Should I just be showing and hiding the console as they navigate around instead?
Was This Post Helpful? 0
  • +
  • -

#20 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 08:16 AM

Showing and hiding might be the better way to go, assuming you want to maintain some kind of state (or skip re-loading data) from when you hide it and when you show it. Otherwise it's not overly important.
Was This Post Helpful? 0
  • +
  • -

#21 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 08:18 AM

View PostCurtis Rutland, on 19 September 2013 - 10:16 AM, said:

Showing and hiding might be the better way to go, assuming you want to maintain some kind of state (or skip re-loading data) from when you hide it and when you show it. Otherwise it's not overly important.


Okay, that's good to know. The console doesn't retain any data, it just contains buttons that take the user to different windows.
Was This Post Helpful? 0
  • +
  • -

#22 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 09:44 AM

I've been stuck on this for a few hours now and need to get it working before I move on to properly dumbing down my forms and separating my data handling.

I have a datagrid that displays records from the database. The user can select a record (I set SelectionMode to Single). I want to take that selection and use it to form a Delete Query to delete the record from the database.

How to I cast this data into something usable to form a query? I tried making a new Device object,

            Device toDelete = new Device();
            toDelete = (Device)dataGridData.SelectedItem as Device ;


Which didn't work. I checked the documentation of the SelectedItem property and it seemed like it was a generic object so I thought it would work. Of course this boils down to my lack of understanding on how the DataGrid handles data. I passed the data in thusly:

 DataManager db = new DataManager();
            DataTable data = new DataTable();
            string query = "SELECT devHostName, devLocation, devType FROM Devices;";
            data = db.GetDataTable(query);

            SQLiteDataAdapter adapter = new SQLiteDataAdapter();
            SQLiteCommand mycommand = new SQLiteCommand();
            dataGridData.ItemsSource = data.DefaultView;
            this.dataGridData.DataContext = data;



I know, I know.. I'll fix it I promise! But boss wants a quick demo in the AM and I need to get this function working before I show him the demo. I've already done some refactoring (omg so awesome!!) and implemented other suggestions here.

So.. what is the logic I need to use here? I need to pull the data in the selected row out in a way that is usable in crafting a query. I thought about a string, but that won't work because I'll never know how long the fields will be, so I can't split a string into fields.

EDIT:

I tried putting the selection into a string to see what it's returning, and it came back as a System object instead of the row contents I was expecting.. (see image).. does that mean I did the DataGrid wrong? It shows my records properly, but I'm stumped.

Posted Image

This post has been edited by synlight: 19 September 2013 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#23 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 10:51 AM

This makes sense, because you bound a DataTable to your DataGrid, right? So the selected item will be a row of that data, which is of type System.Data.DataRowView. So, don't try to assign it to a string, use DataRowView. DataRowView has an indexer defined as well, so you can get the values from the columns out by using the column name. Here's an example:

DataRowView row = (DataRowView)dataGridData.SelectedValue;
string hostName = (string)row["devHostName"];


Now, later if you switch things around to use proper models and lists of models instead of DataTables, you won't have to cast to a DataRowView, you can just cast to whatever type your model is.
Was This Post Helpful? 1
  • +
  • -

#24 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 10:55 AM

Ahhh that makes sense. This goes back to my whole trying to walk before I crawl thing, with understanding how the data and the application interact. I'm going to try to find a good article tonight so I can get a better grasp on it.

Thank you, @Curtis.
Was This Post Helpful? 1
  • +
  • -

#25 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 11:00 AM

Yeah, it's another one of those things that comes with experience. I typically don't try to use .ToString for much other than the built-in types I know it's already defined for, like numbers and such. Because for most classes, they never overrode the ToString method, so it just falls back the the default behavior defined in System.Object, which is to print its Type.

It can be helpful though. When you see a type name like that, it's usually worth looking it up on google, which should take you to the MSDN, which will have all kinds of handy info to help out.

I have to say, this has been my favorite thread to participate in for quite some time. I love to teach; I just don't usually have such a receptive audience. Always good to know that your advice is appreciated and actually used :D
Was This Post Helpful? 1
  • +
  • -

#26 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 11:07 AM

Well I'm glad helping me is helping you LOL. I want to be a GOOD programmer. I don;t want to just hack and slash and hope for the best. I want to really GET it, know what I mean? Especially being straight out of college with no one else to ask.. I landed a job in a one (wo)man shop. And soon we will hire more people, which means I will likely be managing people with more experience. Gonna take lunch now, I got all caught up trying to get this working and forgot to eat. I love it though.
Was This Post Helpful? 1
  • +
  • -

#27 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 351
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: Grade my connection logic..

Posted 19 September 2013 - 11:42 AM

IF you are working with databases, datatable, datarow ... etc read about ADO.NET also Entity framework is part of ADO.NET since .NET framework version 3.5 SP1 so you don't need extra installation.
Was This Post Helpful? 0
  • +
  • -

#28 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 11:45 AM

I'd hold off on the EF stuff until after you finish this project. I love EF and use it for most of my projects that require DBs, but it's not easy to yank all the old ADO.NET logic out and put all the EF stuff in, so it's better to stick with what you started with. But definitely read up on it later, because it's very cool stuff.
Was This Post Helpful? 0
  • +
  • -

#29 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Grade my connection logic..

Posted 19 September 2013 - 12:26 PM

SWEET it's working. THANK YOU.

Which is a miracle because I kept goldfishing reading the links you guys are posting.

Okay.. I have another question.. a proper procedures thing.

I have my device class that is made of several fields. The PK(and only unique) field is the DID (deviceID - autoincrement int). Technically you could have machines on a large network with the same host name and location, etc.

So I'm worried that eventually, with a couple hundred records, somebody could have a machine that had the same hostname, location, and device type. Then, if they tried to delete one of them, both of them would be deleted. That's not good!

The workaround I was thinking of, was to add the DID to the dataset, but hide the column in the datagrid. That way the selected device would always be the right device to delete. Does that sound like a good plan? Can i even hide columns from the datagrid?

EDIT: Maybe it would be easier to add the IP Address field? that would be unique, and help users make sure they had the right device.

This post has been edited by synlight: 19 September 2013 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#30 Curtis Rutland  Icon User is offline

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


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Grade my connection logic..

Posted 19 September 2013 - 12:42 PM

Quote

The workaround I was thinking of, was to add the DID to the dataset, but hide the column in the datagrid. That way the selected device would always be the right device to delete. Does that sound like a good plan? Can i even hide columns from the datagrid?


Yep, that's exactly what you should do. Basically any operation you want to do on individual records should be done by using that record's ID, such as deletes and updates. That way, you're not doing any kind of fuzzy logic at all. You're working against a unique key, so you know you're not messing with any other records.

Here's a general database tip: every table needs a primary key unless you can come up with a really good reason for it not to have one. It just makes things easier. Keep that in mind during your design phase and it'll make your life easier later while you're doing the actual development.

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.

BTW, here's a good link for DataGrid:

http://msdn.microsof...s.datagrid.aspx

Check out the sample code at the bottom.
Was This Post Helpful? 0
  • +
  • -

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