Home > Net >  Inserting multiple queries in table instead of one C#
Inserting multiple queries in table instead of one C#

Time:06-25

I am using winform C#, database name "School" my "fees" table has 2 columns, stu_id,fees

The issue I am facing is that it adds multiple (same) entries into the database instead of single. I have code on other forms but I don't know why is this happening here, any help would be appreciated.

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 SchoolManagementSystem
{
    public partial class Fees : Form
    {
        public Fees()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DRAGON\SQLEXPRESS;Initial Catalog=School;Integrated Security=True;");
            con.Open();

            try
            {
                string str = " INSERT INTO fees VALUES('"   textBox1.Text   "','"   textBox2.Text   "')";

                SqlCommand cmd = new SqlCommand(str, con);
                cmd.ExecuteNonQuery();

                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    MessageBox.Show("Your Fees Submitted..");
                    this.Hide();
                    Home obj2 = new Home();
                    obj2.ShowDialog();
                }
                this.Close();
            }
            catch (SqlException excep)
            {
                MessageBox.Show(excep.Message);
            }
            con.Close();

        }
        private void textBox1_MouseLeave(object sender, EventArgs e)
        {
            textBox2.Text = "";
            SqlConnection con = new SqlConnection(@"Data Source=DRAGON\SQLEXPRESS;Initial Catalog=School;Integrated Security=True;");
            con.Open();
            if (textBox1.Text != "")
            {
                try
                {
                    string getCust = "select name,standard,medium from student where std_id="   Convert.ToInt32(textBox1.Text)   " ;";      // saving new custmer info

                    SqlCommand cmd = new SqlCommand(getCust, con);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        label9.Text = dr.GetValue(0).ToString();
                        label6.Text = dr.GetValue(1).ToString();
                        label7.Text = dr.GetValue(2).ToString();
                    }
                    else
                    {
                        MessageBox.Show("Sorry '"   textBox1.Text   "' This Registration Id is Invalid, Please Insert Correct Id");
                        textBox1.Text = "";
                        textBox2.Text = "";
                    }
                }
                catch (SqlException excep)
                {
                    MessageBox.Show(excep.Message);
                }
                con.Close();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Fees_Load(object sender, EventArgs e)
        {

        }
    }
}

when I enter fees from front end, it adds same 2 rows into database instead of 1.

enter image description here

CodePudding user response:

Because you're executing your INSERT statement twice:

cmd.ExecuteNonQuery();

SqlDataReader dr = cmd.ExecuteReader();

Why? Why do you even need the SqlDataReader? Just execute the INSERT once:

cmd.ExecuteNonQuery();
MessageBox.Show("Your Fees Submitted..");

If you want to confirm that a row was inserted, ExecuteNonQuery returns the number of rows affected:

var rows = cmd.ExecuteNonQuery();
if (rows != 1)
{
    // handle error
}
else
{
    MessageBox.Show("Your Fees Submitted..");
}

CodePudding user response:

You are executing the command twice here:

cmd.ExecuteNonQuery();

SqlDataReader dr = cmd.ExecuteReader();

Why are you calling ExecuteReader when there's nothing to read? ExecuteReader is for when you execute a SELECT statement with multiple columns and/or multiple rows and you want to read the result set. For an INSERT statement, you only need to call ExecuteNonQuery... because it's not a query.

  • Related