5 Replies - 1626 Views - Last Post: 01 January 2015 - 02:41 AM Rate Topic: -----

#1 galaxybomb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 18-December 14

Fatal error during command execution

Posted 31 December 2014 - 04:19 PM

hi guys does anyone know why i keep getting the above error. below is my code ty
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data.SqlClient;

namespace P.I_Gene
{
    public partial class frmNewClient : Form
    {
          private object p;

                   MySqlConnection connection;
         
         


        public frmNewClient()
        {
            InitializeComponent();
        }

           public frmNewClient(object p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }


     

     

        private void kryptonDateTimePicker1_CloseUp(object sender, ComponentFactory.Krypton.Toolkit.DateTimePickerCloseArgs e)
        {
            kryptonDateTimePicker1.Value = DateTime.Now;
        }

        private void btnSubmitClients_Click_1(object sender, EventArgs e)
        {

            var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;
           

            connection = new MySqlConnection(connectionString);
            
            
                connection.Open();
            
            try
            {

               string Query = "INSERT INTO t_pi_clients(Clearing_Agent_ID,ClientCode,ClientName,PostalAdd,Telephone,Fax,EmailAdd1,EmailAdd2,EmailAdd3,Website,TotalDeposit,AccountBal,ChargeRate,Deleted,EntryDate)VALUES(@Clearing_Agent_ID, @ClientCode, @ClientName, @PostalAdd, @Telephone, @Fax, @EmailAdd1, @EmailAdd2, @EmailAdd3, @EntryDate, @Website, @TotalDeposit, @AccountBal, @ChargeRate, @Deleted, @EntryDate)";
     
               MySqlCommand cmd = new MySqlCommand(Query, connection);
               
                cmd.Parameters.AddWithValue("@ClientCode", txtboxClientCode.Text);
                cmd.Parameters.AddWithValue("@ClientName", txtboxClientName.Text);
                cmd.Parameters.AddWithValue("@PostalAdd", txtboxPostalAdd.Text);
                cmd.Parameters.AddWithValue("@Telephone", txtboxTelephone.Text);
                cmd.Parameters.AddWithValue("@Fax", txtboxFax.Text);
                cmd.Parameters.AddWithValue("@EmailAdd1", txtboxEmail1.Text);
                cmd.Parameters.AddWithValue("@EmailAdd2", txtboxEmail2.Text);
                cmd.Parameters.AddWithValue("@EmailAdd3", txtboxEmail3.Text);
                cmd.Parameters.AddWithValue("@Website", txtboxWebsite.Text);
                cmd.Parameters.AddWithValue("@TotalDeposit", txtboxTotalDepo.Text);
                cmd.Parameters.AddWithValue("@AccountBal", txtboxAccountBal.Text);
                cmd.Parameters.AddWithValue("@ChargeRate", txtboxChargeRate.Text);
                cmd.Parameters.AddWithValue("@EntryDate", kryptonDateTimePicker1.Text);
              
                int result =  cmd.ExecuteNonQuery();



                MessageBox.Show("Entry Saved");






            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }
            }

            txtboxClientCode.Text = string.Empty;
            txtboxClientName.Text = string.Empty;
            txtboxPostalAdd.Text = string.Empty;
            txtboxTelephone.Text = string.Empty;
            txtboxFax.Text = string.Empty;
            txtboxEmail1.Text = string.Empty;
            txtboxWebsite.Text = string.Empty;
            txtboxTotalDepo.Text = string.Empty;
            txtboxAccountBal.Text = string.Empty;
            txtboxChargeRate.Text = string.Empty;
        }




        }
    }



Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error during command execution

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Fatal error during command execution

Posted 31 December 2014 - 04:31 PM

You should temporarily disable the try-block so that you could see, and post, the full error details.

You are trying to insert @EntryDate twice, but it doesn't match up with two fields of the table.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: Fatal error during command execution

Posted 31 December 2014 - 04:32 PM

A couple things:
  • You've got over a hundred lines of code an haven't told us WHERE Visual Studio is breaking. That sounds like the tutorials on how to debug would help you a lot. See the FAQ below.
  • Your use of the textboxes as your variables is just a horrible practice. Please see the properties tutorial linked in my signature block.
  • You really want to get the database stuff out of your main code. It belongs in a data access layer. Your objects and GUI have no business knowing anything about the database: They just need to be fed objects and not care where they came from.
  • Data/Database-centric application like this REALLY lend themselves to using WPF instead of 20 year old WinForms, because you can do DataContext binding from the GUI to the data object and have the GUI update automatically when the object property changes and vice versa: Two way binding for free with no C# code for you to write. So you might consider leaving the 1980's behind (except for the awesome music) and join everyone else in the 21st century and learn WPF.



Looking at your code you have some bad practices in play that need your immediate attention.
These will form bad habits quickly and should be worked on. This is part of what I mean by "learn foundation concepts first".

Problem 1: Use of GUI controls as your variables.
You have numerous places where you are doing things like: SomeMethodCall(TextBox43.Text);
Your textboxes and other GUI elements are not meant to be your variables to hold values. They exist only to be a way for the user to interact with the data held in the logic code *behind* the GUI. You should have properties bound to the GUI. When the user changes something in a textbox it updates the property in your code. When the property gets a new value, the GUI is updated. But your logic always uses the property not the GUI element.
There is a tutorial on properties linked in my signature block.

Problem 2: This bit about button1 and textbox14 is just horrible. If you are working in old 1980's WinForms for Win98 and WinXP programs then name your control as soon as you make it before doing anything else with it. txtbxFirstName is so much easier to maintain than textbox32. Same with "frmConnectionSettings" instead of "form2" or "wndCustomerDetails" than "window4". If you are making programs for current OSes like Win7/8 then you should be working in WPF where the controls don't need to be named 90% of the time, but the same practice of giving meaningful names should still be followed in the few cases where they are needed.

Problem 3: Don't put meaningful operational code in GUI control handlers. You have a lot of places where the work is being done in the button event handler instead if in a dedicated method. Instead you want that handler to just direct action to the first methods, not actually perform the actions.
btnSave_Click(object sender, eventargs e)
{
   SaveSettings(); // Call the save method
   SavePlayers();
   SaveWindowGeometry();
   // Do NOT put all the save code here
}

This way you can call the same save methods independently form code without having to either duplicate code (evil practice) or without calling the button handler. If you are using WinForms then you have a .Click event handler. If you are writting in WPF then don't call the .Click event but instead create Commands and activate those. The .Click handler in WPF is kind of a carry-over to give WinForms coders a comfortable migration path but is not considered the way a modern profession WPF coder works.

Problem 4:
Eye sore code.
Take the extra 2 minutes to clean up your code to make it easy to read, easy to follow, and most important, easy to manage.
  • Kill all those dead blank lines at the ends of methods.
  • Wrap groups of methods that go together in #region blocks so you can find all your sending code and receiving code easily for example.
  • Get your lines aligned. Control-K, Control-D will do that for you (Think of it as Klean Document)


Problem 5: Decent naming
Methods like private void ClientSession(object sender, eventargs e); don't follow established naming style guides.
A method performs an action. By convention for decades methods are named as verbs to show what they do. Nouns like ClientSession would be presumed to be a property by any other coder looking at your code. private void CreateClientSession(object sender, eventargs e); on the other hand shows that it does something (a method) not that it is something (a variable).

I'm not going to go into a long critique of your code. And I don't want you to feel like I'm just out to pick on you. I'm just pointing out a couple of many foundation concepts you didn't pickup up in the early stages of learning C#. You can't leap-frog the learning process by trying to build a program in your first month. Nobody can. Its not just you. Nobody in the first month of a new discipline has enough knowledge to jump in and start designing finished solutions: Not carpentry, not automotive design, not software engineering. C# is a foreign language with its own syntax. You aren't going to say "I know no Russian. I'm going to write a Russian mystery novel as my vehicle to learn the language." Same here with programming.

I urge you to take a step back. Pick up a "Learn C# in 30 days" type book and work your way through it cover to cover; don't use it a like a reference dictionary when you get stuck. There are several in a thread pinned to head of the C# forum. Plus there are countless C# beginner tutorial series around just like the C# Learning Series - tutorials here on DIC. There are also numerous "My First C# Program" tutorials like this one that will help you with the design and OOP concepts that are vital to good programming.


See FAQ # 2 "How do I debug?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated December 2014
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Fatal error during command execution

Posted 31 December 2014 - 04:33 PM

cmd.Parameters.AddWithValue("@EntryDate", kryptonDateTimePicker1.Text);

You should also use the (date) Value of the DTP, not its text representation.
Was This Post Helpful? 2
  • +
  • -

#5 galaxybomb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 18-December 14

Re: Fatal error during command execution

Posted 31 December 2014 - 06:16 PM

tanx and i think its best to move to wpf but first of all is there no way of solving that problem becos i used same insert method in a previous project and works fine but doesnt work in this project but they both point out to one database

Attached image(s)

  • Attached Image

This post has been edited by tlhIn`toq: 01 January 2015 - 07:07 AM
Reason for edit:: Removed massive quote of tlhIn`toq's post

Was This Post Helpful? 0
  • +
  • -

#6 galaxybomb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 18-December 14

Re: Fatal error during command execution

Posted 01 January 2015 - 02:41 AM

managed to solve the problem, had to do with my insert statement

@Andrewsw you are right about the value so i used
cmd.Parameters.AddWithValue("@EntryDate", kryptonDateTimePicker1.Value);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1