David Godistei

David Godistei

  • 1.3k
  • 433
  • 1.6k

Error in saving data from the child form into the source table

Nov 3 2024 8:59 AM

From assistance on the Forum I have successfully created a link between codes on both the parent and the child forms in the Sales table. But when I enter data in the child form and want to save it into the table, it displays the following error message; and thus makes my work incomplete:

" System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' "

I want to attach here with both code windows of the parent and child forms and the image of the process for detail assistance.

The Parent code window:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BusinessManagement
{
    public partial class SalesList : Form
    {
        public SalesList()
        {
            InitializeComponent();
            
        }

        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        private void SalesList_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            con.Open();

            cmd = new SqlCommand("SELECT Code, ItemCategory, Description, UnitSize FROM Sales", con);
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            dataGridView1.DataSource = dt;
            con.Close();
        }

        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            con.Open();

            try
            {

                cmd = new SqlCommand("SELECT Code, ItemCategory, Description, UnitSize FROM Sales Where Code LIKE '%' +@Code+'%' OR ItemCategory LIKE '%' +@ItemCategory+ '%' OR Description LIKE '%' +@Description+ '%' OR UnitSize LIKE '%' +@UnitSize+ '%'", con);

                cmd.Parameters.AddWithValue("@Code", txtSearch.Text);
                cmd.Parameters.AddWithValue("@ItemCategory", txtSearch.Text);
                cmd.Parameters.AddWithValue("@Description", txtSearch.Text);
                cmd.Parameters.AddWithValue("@UnitSize", txtSearch.Text);

                da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                SqlDataReader reader = cmd.ExecuteReader();
                dt = new DataTable();
                dt.Load(reader);

                dataGridView1.DataSource = dt;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            con.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                // Get the selected unique code from the clicked row
                string code = dataGridView1.Rows[e.RowIndex].Cells["Code"].Value.ToString();
                // Open the child form and pass the unique code
                SalesForm childForm = new SalesForm(code);
                childForm.ShowDialog();
            }
        }
    }
}

 

The child form code window:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BusinessManagement
{
    public partial class SalesForm : Form
    {
        private string code;

        DataRow row;

        // Constructor accepts the unique code passed from the parent form
        public SalesForm(string code)
        {
            InitializeComponent();

            this.code = code;
            codeTextBox.Text = code;
        }

        // Add logic to save entered data to the database
        private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);

            cmd.Connection = con;
            con.Open();
            cmd.CommandText = "INSERT INTO Sales (SaleDate, Code, SaleQty, SalePrice, TotalSales, Damaged, Comments) VALUES(@SaleDate, @Code, @SaleQty, @SalePrice, @TotalSales, @Damaged, @Comments)";
            cmd.Parameters.AddWithValue("@SaleDate", dateDateTimePicker.Text);
            cmd.Parameters.AddWithValue("@Code", codeTextBox.Text);
            cmd.Parameters.AddWithValue("@SaleQty", saleQtyTextBox.Text);
            cmd.Parameters.AddWithValue("@SalePrice", salePriceTextBox.Text);
            cmd.Parameters.AddWithValue("@TotalSales", totalSalesTextBox.Text);
            cmd.Parameters.AddWithValue("@Damaged", damagedTextBox.Text);
            cmd.Parameters.AddWithValue("@Comments", commentsTextBox.Text);          
            cmd.ExecuteNonQuery();

            // Close Connection
            con.Close();

            //Create a DataRow to add to the DataSet

            DataSet ds = new DataSet();
            ds.Tables["Sales"].Rows.Add(row);

            //Accept changes to the DataSet
            ds.AcceptChanges();

            //Display a success message
            MessageBox.Show("Record saved successfully!");
        }

        private void SalesForm_Load(object sender, EventArgs e)
        {

            
        }
    }
}

 

Thanks for usual assistance.


Answers (1)