Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys, i have a data grid view that i would like to grab an item from and then use it as a selection. I do this because in the form i only want to save data that is already in the database as my business rules are very strict, so to save people having to type it in perfectly, they just have to select it from the data grid view and their selection will be saved into the database. So in previous cases i have implemented it like this to grab the person ID and it works fine.
the code i used for person ID is as follows:
C#
if (dgvPersonIDMedical.SelectedCells.Count !=0)
            {
                int selectedrowindex1 = dgvPersonIDMedical.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow1 = dgvPersonIDMedical.Rows[selectedrowindex1];
                medPersonID = Convert.ToInt16(selectedRow1.Cells[0].Value);
            }





the one i am trying to use now is a string so i tried a convert.to string and it is not working, here is my code with the insert statement

C#
if (dgvMedical.SelectedCells.Count !=0)
            {
                int selectedrowindex1 = dgvMedical.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow1 = dgvMedical.Rows[selectedrowindex1];
                conditionType = Convert.ToString(selectedRow1.Cells[0].Value);
            }


            try
            {
                medicalTableAdapter.Insert(Convert.ToInt32(medPersonID), conditionType, medicareTXT.Text, doctorTXT.Text, healthFundTXT.Text, memberNumberTXT.Text, ambulancefundTXT.Text, medicationTXT.Text);
            }

            catch (Exception)
            {
                MessageBox.Show("Error. That ID already exists within the medical table");

            }




my code comppiles fine but keeps bringing up the catch block which means i messed something up somewhere, can anyone tell me where?

thanks for your time!
Posted

1 solution

No. Sorry.

But - you can tell yourself where it is.

If you can cause a failure in development, then put a breakpoint on the MessageBox line, and examine the exception itself. SQL server is pretty good at reporting problems, and it can help you quite a bit with what it causing it. You can also go back and execute the Insert again, this time single-stepping into the method until you get the problem. There is also the big-hammer approach: VS Menu, "Debug". Select "Exceptions" and make sure every single checkbox has a tick, both "Thrown" and "User unhandled". Press OK. You will now get VS stopping whenever an exception is thrown, so you can see exactly what is causing it. This may be a PITA depending on how defensively your app is written...

If it only occurs in production, then add code before the message box to log everything (and I mean everything) to a text file so you can read it later (it is a good idea to log errors anyway, it makes debugging easier). It's not as simple to find the problem that way, but it may give you enough to replicate the problem in development conditions.
 
Share this answer
 
Comments
Manisha Tambade 17-Nov-11 9:34am    
Very good guidence,To become a Best Developer one should follow these characteristic.
Member 7791974 17-Nov-11 9:46am    
thank you very much, i will give this a go now!
Member 7791974 17-Nov-11 9:57am    
i have had a look at the code and added a breakpoint, the code that i added had this bit in it that was flagged with a red color
this {MSMS_v2_.Form1, Text: Menslink Stakeholder Management System, Logged in as : SLSTA2ACD\Solsta} MSMS_v2_.Form1

inside this there was another base class and another base class, and in each consecutive one there was another, it seemed to go on forever
OriginalGriff 17-Nov-11 10:19am    
Damn it - what a time for CP hamsters to pull the plug on the DB system... Oh well, lets try to answer that again...

You aren't intereested in teh form content at the moment - you need to know more about the exception, and what is triggering it. Modify your "catch (Exception)" line to "catch (Exception ex)" and leave the breakpoint on the MessageBox. When the breakpoint triggers, look at the "ex" object - it will give you a some (hopefully a fair amount of) information about what problem has been detected. You can then use this to focus down through the code to spot what problem has been detected - this may not be the source of the problem directly, but it should give you enough to start working you way back up to what data is causing the problem to be detected.
Let me know what you find!
Member 7791974 17-Nov-11 10:59am    
Yeah i keep getting this infinite thing again, same as my previous post, when i click on the plus near this, it has another error saying {MSMS_v2_.Form1, Text: Menslink Stakeholder Management System, Logged in as : SLSTA2ACD\Solsta} MSMS_v2_.Form1

then within that, and it goes on forever.....totallly confused

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900