Communication with a database

Started by
17 comments, last by NDraskovic 12 years, 5 months ago


The compiler doesent report any errors or warnings, but the text that is displayed is probobly the text of the query not the result it should produce.

Probably? Can't you post the message? I'm effectively stabbing in the dark, as I don't have a C# compiler handy.

One thing I do see is that you are selecting two values in the query, but asking it to return String instances. I'm not sure what you expect the result to be.


Datum is a Date (although I made it a nchar[20] and I fill it automaticly by using C#-s DateTime.Now() method)...
[/quote]
Storing the Datum as a date type will allow you to do data comparisons, as opposed to string comparisons. It will also allow you to use the NOW() SQL function, which is a little easier than creating a String from
[/quote]

Ok, again I'm sorry for posting such incomplete data, but like I said, the workstation I'm writing the program on has no Internat accesss, so I have to manualy retype everything into these posts. When I execute that instruction, the following is writen in textBox : System.Data.Linq.'SqlClient.SqlProvider+One TimeEnumerable'1[System.String].
You asked what result do I expect - I would like a string, or an array of strings, from which I can compose a sentence understandable to humans and display it in the textBox. For time being I'm doing this by clicking on a button, but in the end it should be done automaticly - I will use the Date type like you suggested.
Advertisement
Ok I managed to display the data the way I wanted - I used DataGridView control, it proved to be much simpler then using the textBox control. The problem I'm having now is that when I add data to my database (or more precise to my dataset), it doesent show in DataGridView. I'v tried to use the DataGridView.Refresh method, and I can see that the control is redrawn, but the new data still isn't displayed (I know that it is properly added to the database because when I close and run my application again the new data is displayed normaly). Please advise.

Tnx
Hello Ndraskovic,


Refresh only redraws the actual graphical object,
you probably want to call DataBind() after every action that adds or deletes something from the database.

Here's a great quick start video from (my hero) Scott Gu, about easily filling a gridview with data using LINQ:
http://www.scottgu.com/blogposts/video/linqtalk1.wmv


Cheers,
Daniel
Daniel Molnar
MCTS: MS SQL 2008 and .NET Framework 4
[email="daniel@murmix.com"]daniel@murmix.com[/email]

Murmixsoft
http://www.murmix.com
I haven't used WinForms, so I can't give concrete advise about what you should be doing. However it sounds like you are only re-drawing the last content loaded, you aren't re-loading the new data from the database.

You can set a breakpoint on the line which calls into the database, see if it gets hit when you call the refresh. If so, then I am incorrect. If it doesn't get triggered, then that is your problem.
You are right, it only redraws the control, but it has no influence over the data it shows. I tried to find a solution for over 3 hours now, and I found a lot of people with the same problem - I tried to apply their solutions but they dont work either. This is what I'v tried:

- dataGridView.Refresh, datagridView.Invalidate and dataGridView.Update methods - they all produce the same result (the control gets redrawn, with no changes to the displayed data)
- emptying the DataSource and reloading it by:

dataGridView.DataSource = null;
dataGridView.DataSource = probaDataSet.ProbnaTablica;


This deletes everything from the display area and only leaves the column names and a blank row.

So I agree that I have to refresh the DataSet somehow - can you tell me how it's done. I found methods AcceptChanges() (I'v put it right after the line where I insert the data into the database) and GetChanges() (into button2_Click method) but it still doesent work (I get the same result as with emptying the DataSource).

Ok, again I'm sorry for posting such incomplete data, but like I said, the workstation I'm writing the program on has no Internat accesss, so I have to manualy retype everything into these posts. When I execute that instruction, the following is writen in textBox : System.Data.Linq.'SqlClient.SqlProvider+One TimeEnumerable'1[System.String].
You asked what result do I expect - I would like a string, or an array of strings, from which I can compose a sentence understandable to humans and display it in the textBox. For time being I'm doing this by clicking on a button, but in the end it should be done automaticly - I will use the Date type like you suggested.


I don't know enough about Windows forms to get your current problem down. I've only done a little bit in WPF, which I understand to be slightly different from windows forms.

Anyway, the reason you are getting this output is because ExecuteQuery is returning an IEnumerable holding strings. You are calling ToString on the IEnumerable, and the default ToString method for C# objects just prints out the object's class name, which is exactly what it did as far as I can tell. What you want to do is call the GetEnumerator() function of the IEnumerable you get back (tmp) and iterate through that until you run into the end.

I'm not sure if this will give yout he behavior you are expecting, but you'll get a really long string of IDs text probably. I'm not sure how ExecuteQuery returns rows, you might have to treat each row as an array also.

What is your end goal? A game?

[quote name='NDraskovic' timestamp='1321545501' post='4884994']
Ok, again I'm sorry for posting such incomplete data, but like I said, the workstation I'm writing the program on has no Internat accesss, so I have to manualy retype everything into these posts. When I execute that instruction, the following is writen in textBox : System.Data.Linq.'SqlClient.SqlProvider+One TimeEnumerable'1[System.String].
You asked what result do I expect - I would like a string, or an array of strings, from which I can compose a sentence understandable to humans and display it in the textBox. For time being I'm doing this by clicking on a button, but in the end it should be done automaticly - I will use the Date type like you suggested.


I don't know enough about Windows forms to get your current problem down. I've only done a little bit in WPF, which I understand to be slightly different from windows forms.

Anyway, the reason you are getting this output is because ExecuteQuery is returning an IEnumerable holding strings. You are calling ToString on the IEnumerable, and the default ToString method for C# objects just prints out the object's class name, which is exactly what it did as far as I can tell. What you want to do is call the GetEnumerator() function of the IEnumerable you get back (tmp) and iterate through that until you run into the end.

I'm not sure if this will give yout he behavior you are expecting, but you'll get a really long string of IDs text probably. I'm not sure how ExecuteQuery returns rows, you might have to treat each row as an array also.

What is your end goal? A game?
[/quote]

Like I said this is just a test, since I never worked with connecting databases to WinForms (or anything else for that matter) - but I chose a real life situation to test on (a friend of mine told me about a piece of software he is working on so I wanted to try it out). The (semi)final result I would like to accomplish is the application with following sections:
1. A section where a user can write textual data (specifically attributes of upcoming "events") and that would be inserted into a database that has the appropriate schema - I managed this with the ExecuteCommand that I wrote in earlier post.
2. A display area (I drew a blank with multiline textBox, so I switched to DataGridView, managed to get the data display to work, and the problem of refreshing the data) where a user will see the data from the database (always a constant number of rows) with the possibility of highlighting and removing the "events" that occured. When user removes one "event", the program automaticlly takes another from the database and displays it (the events are ordered by DateTime variable so that the event that should happen first is at the top of the display area, and the latest one is at the bottom).
I know that this is not a game material (at least not in this form), but I'm often on Gamedev's forums, so I hoped someone could help me. I'v been fighting with this problem for the better part of the afternoon, and still havent resolved it, so if you have any suggestions, ideas or anything (in any form you can think of) please share, because it's driveing me mad :D

Tnx again to everybody

Like I said this is just a test, since I never worked with connecting databases to WinForms (or anything else for that matter) - but I chose a real life situation to test on (a friend of mine told me about a piece of software he is working on so I wanted to try it out). The (semi)final result I would like to accomplish is the application with following sections:
1. A section where a user can write textual data (specifically attributes of upcoming "events") and that would be inserted into a database that has the appropriate schema - I managed this with the ExecuteCommand that I wrote in earlier post.
2. A display area (I drew a blank with multiline textBox, so I switched to DataGridView, managed to get the data display to work, and the problem of refreshing the data) where a user will see the data from the database (always a constant number of rows) with the possibility of highlighting and removing the "events" that occured. When user removes one "event", the program automaticlly takes another from the database and displays it (the events are ordered by DateTime variable so that the event that should happen first is at the top of the display area, and the latest one is at the bottom).
I know that this is not a game material (at least not in this form), but I'm often on Gamedev's forums, so I hoped someone could help me. I'v been fighting with this problem for the better part of the afternoon, and still havent resolved it, so if you have any suggestions, ideas or anything (in any form you can think of) please share, because it's driveing me mad :D

Tnx again to everybody


I'd recommend actually switching to WPF over WinForms before getting too deep in. WPF seems much more data conscious to me. It's also a bit more confusing, but you can do some pretty cool stuff with it. At least worth a quick WPF vs windows forms google expedition. :-p
OK just to wrap things up - I finally managed to get the problem solved by using a third DataSet that I fill with the data from the database itself, and then "change" the DataSource of the dataGridView which then displays the new data correctly.

I'd like to thank everybody who commented on this post and thanks for your patience.

This topic is closed to new replies.

Advertisement