Click here to Skip to main content
16,021,449 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da;
         
       String ConnectionString = @"Data Source=SHAKIR-PC\SQLEXPRESS;Initial Catalog=trydb;Integrated Security=True;Pooling=False";
        String cnnStr;
        SqlCommand cmd;
           

                
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'trydbDataSet1.payments' table. You can move, or remove it, as needed.
            this.paymentsTableAdapter.Fill(this.trydbDataSet1.payments);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            int i = 0;
            List<int> ChkedRow = new List<int>();

            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true)
                {
                    ChkedRow.Add(i);
                }
            }

            if (ChkedRow.Count == 0)
            {
                MessageBox.Show("Select one checkbox");
                return;
            }

            foreach (int j in ChkedRow)
            {
                cnnStr = @"INSERT INTO deposits (date,custname,description,chqno,duedate,accname,amount)
                            VALUES ('" + dataGridView1.Rows[j].Cells["dateDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["custnameDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["descriptionDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["chqnoDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["duedateDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["accnameDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["amountDataGridViewTextBoxColumn"].Value + "');";
                try
                {
                    using (SqlConnection cs = new SqlConnection(ConnectionString))
                    {

                        using (cmd = new SqlCommand(cnnStr,cs))
                        {
                            cs.Open();
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            MessageBox.Show("Records Added Succesfully");
                                            

        }
    }
}

what is the problem in this code for the above mentioned error


[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
[no name] 25-Jul-14 7:45am    
What error, where? This is just an unformatted code dump.

To add to previous answers: Never concatenate data into an SQL statement. You leave yourself wide open to SQL injections, conversion problems and so on.

The correct way is to use parameters. Please refer to SqlParameter[^]
 
Share this answer
 
There are many answers to this already in Google[^]. Please give them a try and let us know.

Seems like you are inserting data with larger size than declared in the Database.
 
Share this answer
 
One or more of your data that you are trying to insert is larger than the size you defined for the table field in the database.

Check the size your values and compare them with your table fields in DB. Hope it will fix your issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900