1

Please forgive me if this looks a little strange. I am a fairly new beginner to C#.

I have a linq to sql query that populates various fields on the form. Another section of the form, needs to pull information from another table based off the value of the textbox, "employeeNumberTextBox". Then display that data in a DataGridView.

I have tried to use the following code; however, I think what might be messing me up is the fact that a) there is a date field on the table, and b) there are multiple rows for employeeNumber in this table. Would be an accurate assumption?

        int searchValue = Convert.ToInt32(employeeNumberTextBox.Text.Trim());

        var qry = (from p in Globals.db.Employees_Histories
                   where p.employeeNumber.Equals(searchValue)
                   select p).ToList();

        employees_HistoryDataGridView.DataSource = qry;

I keep receiving an error saying there is a type mismatch; however, I have tried to figure out exactly what that means and it has me a little confused atm.

Any assistance would be greatly appreciated.

Chris Turner
  • 119
  • 1
  • 2
  • 13
  • Could you please provide the exact error description. Also, if you place a breakpoint on the final line - does it get executed? And if it does, how does the data look? It looks like the query is failing before the grid becomes involved at all. – David Hall Aug 20 '12 at 20:19
  • 2
    I'd check the type of employeeNumber and make sure it's a string – AD.Net Aug 20 '12 at 20:19
  • @DavidHall - I apologize. I am not sure what you mean by "place a breakpoint on the final line" The exact error message it gives me is: Input string was not in correct format. Troubleshooting tips: Make sure your method arguments are in the right format, When converting a string to DateTime, parse the string to make the date before putting each variable into the DateTime object. – Chris Turner Aug 20 '12 at 20:22
  • Also - you probably don't need the ToList() - setting the DataSource to the un-materialized query should work (and better) see here for a bit more detail http://stackoverflow.com/questions/11799661/datagridview-allowusertoaddrow-property-doesnt-work/11802151#11802151 – David Hall Aug 20 '12 at 20:22
  • 1
    @ChrisTurner have you used the visual studio debugger before? If not, google for a tutorial on it and learn how. You can't really begin to properly investigate these issues yourself without knowing how to use the debugger. – David Hall Aug 20 '12 at 20:24
  • Ok, using the C# debugger with the break point, it is showing that the query searchValue is returning a "Null" response. I checked the data on the column and changed the code as notated above. Now it is returning a value of zero for searchValue type int – Chris Turner Aug 20 '12 at 20:57
  • @ChrisTurner: Now you know how to fix it. Check whether you have a valid value in the textboxes Text Property. – Shyju Aug 20 '12 at 21:03
  • Ok, I think I have figured out the issue, not sure how to make it work right though. When the form is initially generated the form is blank. Until the user selects an employee from the drop down menu. When the qry attempts to run, there is nothing in the employeeNumberTextBox. Is there a way to cause this query to execute once a name has been selected from the drop down list? – Chris Turner Aug 20 '12 at 21:06
  • @ChrisTurner the typical way of doing this would be handling to combobox SelectedIndexChanged event, but this is only a point solution and there is a lot of 'why things are best done in certain ways' discussion that would serve you better than drip feeding answers - essentially, you will keep hitting these sorts of issues until you understand the .Net framework and WinForms better. If you are doing this for work find someone senior to help. If it is just so you can learn, I'd say keep plugging away, maybe working through some online tutorials. – David Hall Aug 20 '12 at 21:17
  • @DavidHall - Unfortunately there is no one to ask at work and I am working on it on my own. It is mainly just to learn; and someday be able to use in a working environment. – Chris Turner Aug 20 '12 at 21:32
  • @ChrisTurner fair enough - sadly StackOverflow doesn't work very well for learning from the beginning (I find that the best for that is sitting with someone more experienced and asking lots of questions face to face where you can discuss the why), but don't hesitate to ask questions in the future when you have tried a lot of things and nothing is working. Best of luck! – David Hall Aug 20 '12 at 21:38

1 Answers1

0

in my opnion employeeNumber filed is Number and you should Convert to Int by using Int32.Pars like this :

var searchValue = Int32.Parse(employeeNumberTextBox.Text.Trim());

make sure your input is Number

csharp
  • 59
  • 4