Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:10:52+00:00 2026-06-16T05:10:52+00:00

I have a Form where I am inserting a record into the database. There

  • 0

enter image description hereI have a Form where I am inserting a record into the database. There are two tables, table_1 is called members, and table_2 is called Amount.

I am using two SQL INSERT statements to send records to database , because that’s the way I have figured out — there might be other ways, which I don’t know.

When I insert the record I get a message that it is inserted successfully, but when I check the database the inserted record replaces the one present , so I have last record in the DB repeated several times. Please assist.

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

namespace CemiyetAidatSistem
{
    public partial class AddMember : Form
    {
        public AddMember()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("Data Source=My-PC\\SQLSERVER;Initial Catalog=FredericiaDernek;Integrated Security=True");

        private void btnInsert_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            string Sql = "INSERT INTO Uyeleri ( dID, FullName, Address, Mobile, Email, Comments ) VALUES ('" + txtdID.Text + "', '" + txtAdiSoyadi.Text + "','" + txtAddress.Text + "','" + txtMobile.Text + "','" + txtEmail.Text + "','" + txtComments.Text + "')";
            cmd.CommandText = Sql;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            Sql = "INSERT INTO Aidat (dID Year, Amount ) VALUES ('"+ txtdID.Text +"','" + txtYear.Text + "','" + txtAmount.Text + "')";
            cmd.CommandText = Sql;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i] is TextBox)
                {
                    this.Controls[i].Text = "";
                }
            }
            MessageBox.Show("Data Added Scuessfully");
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:10:53+00:00Added an answer on June 16, 2026 at 5:10 am

    I have rewritten your code to correct errors and bad practices

    string connString = "Data Source=My-PC\\SQLSERVER;Initial Catalog=FredericiaDernek;Integrated Security=True";
    
    private void btnInsert_Click(object sender, EventArgs e)
    {
        using(SqlConnection con = new SqlConnection(connString))
        {
            con.Open();
            string Sql = "INSERT INTO Uyeleri (dID, FullName, Address, Mobile, Email, Comments ) " + 
                         "VALUES (@id, @name, @address, @mobile, @email, @comments");
            using(SqlCommand cmd = new SqlCommand(Sql, con))
            {
                cmd.Parameters.AddWithValue("@id", txtdID.Text);
                cmd.Parameters.AddWithValue("@name", txtAdiSoyadi.Text);
                cmd.Parameters.AddWithValue("@address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@mobile", txtMobile.Text);
                cmd.Parameters.AddWithValue("@email", txtEmail.Text);
                cmd.Parameters.AddWithValue("@comments", txtComments.Text);
                cmd.ExecuteNonQuery();
    
                Sql = "INSERT INTO Aidat (dID, [Year], Amount ) VALUES " + 
                      "(@id, @year, @amount)";
                cmd.Parameters.Clear();
                cmd.CommandText = Sql;  // <- missing this in the previous version.....
                cmd.Parameters.AddWithValue("@id", txtdID.Text);
                cmd.Parameters.AddWithValue("@name", txtYear.Text);
                cmd.Parameters.AddWithValue("@amount", txtAmount.Text);
                cmd.ExecuteNonQuery();
            }
        }
    

    What I have changed:

    • The second insert statement is wrong. Missing a comma between first
      and second column
    • Removed the creation of the SqlConnection at the global level
    • Added appropriate using statement to dispose the SqlConnection and
      SqlCommand also in case of exceptions
    • Used parameters for the two insert statements
    • Added square brackets around Year field (Year is a reserved keyword
      in T-SQL)

    Creating a SqlConnection at the global level is bad, because you grab system resources and you don’t dispose them for the lifetime of your application. And the situation could be out of control in case of exceptions not correctly handled.

    Now I have some doubt about your tables. The fields dID (both tables) and Amount are of text type (varchar,nvarchar)?. If they are of numeric type it is necessary to add a conversion before adding the values to the Parameters collection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created some form for inserting data into database and for checking if
I have a PHP script that is inserting a record into a database after
I have a PHP form that is inserting information into a database. It all
I have the following form that I am inserting in to a database with
I have this form which can insert/update values into database table. <div id=settingsdiv style=width:350px;
I have two form,form1 and form2 in C# desktop application.Form1 is for inserting data
I am using an SQLite database, and inserting records into it. This takes a
I have a field in my update form called approve which is using the
I have form in my page, I am using ajax.beginform(). Inside this form I
I have an online form which collects member(s) information and stores it into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.