null error handling with mysql database....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: null error handling with mysql database....

Threaded View

  1. #1
    Join Date
    Feb 2012

    null error handling with mysql database....

    hello all. I am working on a simple program for some medical clients and I'm having trouble with one part. There are two parts to this, one is the web app to view the mysql DB, and the other a windows form to upload data into DB. I am having trouble with the data entry side. I've tried to build in as much internal error handling, since my clients are not very computer literate (simple, simple, simple).
    When I am uploading info i have two cases: either patient exists already or does not, based on searching the DB for existing last names (I'm going to have to change search parameter as well because i will have some patients with same last name but diff first name, but i can address that later). If patient exists then i only need to upload the test report to the 'test' Table, no patient info to 'patient' Table is ness. The part for uploading in case: patient does exists works fine. However if the code checks the DB and does not find a name it returns a null value which throws an error. Null Reference Exception gets returned. This has been frustrating me for hours now.... Anyone help?

    private void button2_Click(object sender, EventArgs e)
                int DoctorID;
                int PatientID=0;
                string connString = "server=localhost;User Id=root;password=Commando;database=flex";
                MySqlConnection conn = new MySqlConnection(connString);
                MySqlCommand selectDocCommand = conn.CreateCommand();
                MySqlCommand selectPatientCommand = conn.CreateCommand();
                MySqlCommand patientTableCommand = conn.CreateCommand();
                MySqlCommand testCommand = conn.CreateCommand();
                MySqlCommand LNameCommand = conn.CreateCommand();
                selectDocCommand.CommandText = "Select DoctorID from (doctors) Where doctors.DoctorUserID = '" + DoctorName.Text + "'";
                selectPatientCommand.CommandText = "Select PatientID From (patients) Where LName = '" + LastName.Text + "'";
                DoctorID = (int)selectDocCommand.ExecuteScalar();
               // PatientID = (int)selectPatientCommand.ExecuteScalar();
                MySqlCommand selectlnameCommand = conn.CreateCommand();
                selectlnameCommand.CommandText = "Select LName From (Patients) Where Lname = '" + LastName.Text + "'";
                string blah = (string)selectlnameCommand.ExecuteScalar();
                if (blah == LastName.Text)  //have to change this! What if two diff patients with same last name...
                    testCommand.CommandText = "Insert into testreports (PDF, TestDate, patients_PatientID, TestType) Values ('fred.pdf', '" + TestDate.Text + "', '3', '" + testType + "')";
                    MessageBox.Show("Test report has been uploaded",
    "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    PatientID = (int)selectPatientCommand.ExecuteScalar();
                    //if (PatientID == null)
                    //    PatientID = 99;
                    //    int newID = PatientID;
                    patientTableCommand.CommandText = "Insert patients (LName, FName, doctors_DoctorID) values ('" + LastName.Text + "', '" + FirstName.Text + "', '" + DoctorID + "')";
                    testCommand.CommandText = "Insert testreports (PDF, TestDate, patients_PatientID, TestType) Values ('test4.pdf', '" + TestDate.Text + "', '" + PatientID + "' '" + testType + "')";
                    MessageBox.Show("A new patient has been added to the database.",
    "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

    Error details:
    System.NullReferenceException was unhandled
    Message=Object reference not set to an instance of an object.
    at ISIDE.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Owner\Desktop\OCR\ISIDE\ISIDE\Form1.cs:line 399
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at ISIDE.Program.Main() in C:\Users\Owner\Desktop\OCR\ISIDE\ISIDE\Program.cs:line 18
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    Last edited by Cimperiali; February 10th, 2012 at 08:25 AM. Reason: Added [Code][/Code] and [/quote][/quote] tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center