Good day all, I am a newbiew in C# application. I am designing a Laundry application, and I want to insert data from textbox, datetime picker and the gridview control into a database table.. I am using SQL Server 2008. This is the window form and controls on it:Form with textbox controls, datetime picker controls and datagridview control.The following code I used for data insertion

While the following code was meant for gridview calculation:

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 System.Data.SqlClient;

namespace CleanWash
{
    public partial class Order : Form
    {


        public Order()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 300; i++)
            {
                dataGridView1.Rows.Add();
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 2 || e.ColumnIndex == 3 || e.ColumnIndex == 4)
                {

                    double cell1 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[2].Value);

                    double cell2 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[3].Value);

                    double cell3 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[4].Value);

                    if (cell1.ToString() != "" && cell2.ToString() != "" && cell3.ToString() != "")
                    {


                        dataGridView1.CurrentRow.Cells[5].Value = (cell1 * cell2) * (1 - (cell3*1/100));
                    }

                }
                label1.Visible = true;
                decimal tot = 0;
                for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
                {
                    tot += Convert.ToDecimal(dataGridView1.Rows[i].Cells[5].Value);
                }
                if (tot == 0)
                {
                    //MessageBox.Show("No Records Found");
                }
                txtNet.Text = tot.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void Save()
        {

            try
            {

                if (dataGridView1.Rows.Count > 1)
                {

                    for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                    {

                        string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();

                        string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();

                        string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();

                        string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();

                        string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        string col6 = dataGridView1.Rows[i].Cells[5].Value.ToString();

                        using (SqlConnection con = new SqlConnection("Data Source=(local);Database='Clean';Integrated Security=yes;"))
                        {
                            string insert = "INSERT INTO Invoice(ItemCode,Descrpt,Qty,Rate,Disc,Total)VALUES(@ItemCode,@Descrpt,@Qty,@Rate,@Disc,@Total)";
                            con.Open();
                            SqlCommand cmd = new SqlCommand(insert, con);
                            cmd.Parameters.AddWithValue("@ItemCode", col1.ToString());
                            cmd.Parameters.AddWithValue("@Descrpt", col1.ToString());
                            cmd.Parameters.AddWithValue("@Qty", col1.ToString());
                            cmd.Parameters.AddWithValue("@Rate", col1.ToString());
                            cmd.Parameters.AddWithValue("@Disc", col1.ToString());
                            cmd.Parameters.AddWithValue("@Total", col1.ToString());

                            cmd.ExecuteNonQuery();

                            con.Close();
                        }
                    }

                }
            }

            catch (Exception ex)
            {

                ex.Message.ToString();

            }

        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
    }
}
Member Avatar for lithium112

Hello col-lino, Welcome to daniweb!
Are you getting any type of error in your code editor when you try and run the insertion of the data?

Well you've a try/catch block. What does the exception tell you?
Don't just look at the Message. Examine the StackTrace and other fields also.
What value does ExecuteNonQuery return? 0, 1 or -1?

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.