Hello guys,
I am building a small accounting system, one of the main functions of this system is to add a new supplier.
I am using Telerik, Rad controls ( trial version) to make some appearance improvements.
Moreover, I have a winform to add a new supplier which contains several radtextboxes, and a datetimepicker. I can insert data to MsAcess and everything is cool, but the problem is when i clear the textboxes and reinsert new data, the same old data is inserted and not the new one i.e ( i opened the application and i added : supplier name (a), supplier last name (a),etc. if i try to insert a new row i.e ( supplier name (b), supplier last name (b). After the insertion i check my database and i found supp name (a), next row supp name (a).
I can only insert new data if i close the whole application and open it back.

Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Data.OleDb;

namespace Global
{
    public partial class AddSupplierForm : Telerik.WinControls.UI.RadForm
    {
        public OleDbCommand cmd = new OleDbCommand();
        public DataTable dTable = null;
        public OleDbDataAdapter dAdapter = null;
        public OleDbDataAdapter da = new OleDbDataAdapter();

        

        public AddSupplierForm()
        {
            InitializeComponent();
        }

        private void AddSupplier()
        {
           
            try {
                if (sFirstName.Text == "" || sLastName.Text == "" || sCellNumber.Text == "")
                {
                    MessageBox.Show(" المساحة المخصصة لادخال المعلومات المرمَزة بعلامة '*' حمراء لا يمكن ان تكون فارغة", "تحذير", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }
                else
                {
                    string fname = Convert.ToString(sFirstName.Text);
                    string lname = Convert.ToString(sLastName.Text);
                    string cname = Convert.ToString(sCompanyName.Text);
                    string caddress = Convert.ToString(sCompanyAddress.Text);
                    string cphone = Convert.ToString(sPhoneNumber.Text);
                    string mphone = Convert.ToString(sCellNumber.Text);
                    DateTime sdate = Convert.ToDateTime(sDate.Text);

                    MyConnection.OpenMyConnection();
                    cmd.CommandText = "select * from [supplier] WHERE [supp_mobile_number]= '" + mphone + "'";
                  
                    cmd.Connection = MyConnection.con;
                    OleDbDataReader dr = cmd.ExecuteReader();

                    if (dr.Read())
                    {
                        MessageBox.Show(" المورَد الذي يتم ادخاله موجود", "تحذير", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                        dr.Close();
                    }

                    else
                   {
                          cmd.CommandText = cmd.CommandText = "INSERT INTO supplier ([supp_first_name], [supp_last_name], [supp_company_name], [supp_company_address], [supp_company_phone], [supp_mobile_number], [supp_add_date]) VALUES (@fname, @lname, @cname, @caddress, @cphone, @mphone, @sdate)";
                        cmd.Parameters.AddRange(new OleDbParameter[]
                            {
                               new OleDbParameter("@fname", fname),
                               new OleDbParameter("@lname", lname),
                               new OleDbParameter("@cname", cname),
                               new OleDbParameter("@caddress", caddress),
                               new OleDbParameter("@cphone", cphone),
                               new OleDbParameter("@mphone", mphone),
                               new OleDbParameter("@sdate", sdate),
                                     
                             });
                         dr.Close();
                        
                        int count = cmd.ExecuteNonQuery();

                        if (count > 0)
                        {
                            MyConnection.CloseMyConnection();
                            MessageBox.Show("تم اضافة المورَد بنجاح", "اضافة مورَد", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                            FillSupplierGrid();
                            sFirstName.Clear();
                            sLastName.Clear();
                            sCompanyName.Clear();
                            sCompanyAddress.Clear();
                            sPhoneNumber.Clear();
                            sCellNumber.Clear();
                            sDate.ResetText();
                            sFirstName.Focus();
                            
                        }
                        MyConnection.CloseMyConnection();
                    }
                    MyConnection.CloseMyConnection();
                }
            }
            catch (Exception ex)
            {
            MessageBox.Show(ex.ToString());
            }
        
        }

        private void FillSupplierGrid()
        {

            MyConnection.OpenMyConnection();
            string query = "SELECT * FROM supplier";

            //create an OleDbDataAdapter to execute the query
            dAdapter = new OleDbDataAdapter(query, MyConnection.con);

            //create a command builder
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

            //create a DataTable to hold the query results
            dTable = new DataTable();

            //fill the DataTable
            dAdapter.Fill(dTable);

            //BindingSource to sync DataTable and DataGridView
            BindingSource bSource = new BindingSource();

            //set the BindingSource DataSource
            bSource.DataSource = dTable;

            //set the DataGridView DataSource
            supplierGrid.DataSource = bSource;
            supplierGrid.Columns[0].IsVisible = false;
            this.supplierGrid.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
            supplierGrid.Columns[1].HeaderText = "اسم الموزَع";
            supplierGrid.Columns[2].HeaderText = "اللَقب";
            supplierGrid.Columns[3].HeaderText = "اسم الشَركة";
            supplierGrid.Columns[4].HeaderText = "عنوان الشَركة";
            supplierGrid.Columns[5].HeaderText = "رقم هاتف الشَركة";
            supplierGrid.Columns[6].HeaderText = "رقم هاتف الموزَع";
            supplierGrid.Columns[7].HeaderText = "تاريخ بدء التعامل";

            MyConnection.CloseMyConnection();
        }

        private void AddSupplierButton_Click(object sender, EventArgs e)
        {
            AddSupplier();
        }

        private void AddSupplierForm_Load(object sender, EventArgs e)
        {
            FillSupplierGrid();
            sFirstName.Focus();
        }


   

      
    }
}

Recommended Answers

All 4 Replies

Replace line 59:

cmd = null;
cmd = new OleDbCommand();
cmd.Connection = MyConnection.con;
cmd.CommandText = "INSERT INTO supplier...

HTH

Your SQL command is incorrect, from the ADO.NET documentation:
The .NET Framework Data Provider for OLE DB and .NET Framework Data Provider for ODBC do not support named parameters for passing parameters to an SQL statement or a stored procedure. In this case, you must use the question mark (?) placeholder, as in the following example., which can be found here, under the "Using Parameters with an OleDbCommand or OdbcCommand" section.

Thank you for your help guys.
Teme64,I replaced the line 59 with yours but nothing happened, still the same data is passed to Database. I think some variable's saving the old data. I am really stuck.

Momerath, can you provide me an example of correct named parameter?

Teme64, it's my fault I replaced the line 59 in other form :P how stupid.
Your solution works perfectly, you're right, I have to nullify the cmd to accept new data.Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.