TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jahangir Khan
NA
27
5.9k
What it wrong with this function ?
May 16 2016 6:45 AM
private
void
btnAdd_Click(
object
sender, EventArgs e)
{
{
con.Open();
//string Sql = "INSERT INTO Members ( MemberId, Name, Address, Mobile, Email ) VALUES " + " (@Id, @name, @address, @mobile, @email)";
string
Sql =
"INSERT INTO Members ( MemberId, Name, Address, Mobile, Email ) VALUES "
+
" (@Id, @name, @address, @mobile, @email); SELECT MemberId FROM Members WHERE NOT EXISTS (SELECT * FROM Members WHERE MemberId = @Id AND Name = @name AND Address = @address AND Mobile = @mobile AND Email = @email)"
;
using
(SqlCommand cmd =
new
SqlCommand(Sql, con))
{
cmd.CommandText = Sql;
cmd.Parameters.AddWithValue(
"@Id"
, tbID.Text);
cmd.Parameters.AddWithValue(
"@name"
, tbName.Text);
cmd.Parameters.AddWithValue(
"@address"
, tbAddress.Text);
cmd.Parameters.AddWithValue(
"@mobile"
, tbMobile.Text);
cmd.Parameters.AddWithValue(
"@email"
, tbEmail.Text);
cmd.ExecuteNonQuery();
//Sql = "INSERT INTO Payments ( MemberId, [Year], [Amount] ) VALUES " + "(@id, @year, @amount)";
Sql =
"INSERT INTO Payments ( MemberId, [Year], [Amount] ) VALUES "
+
"(@id, @year, @amount) SELECT MemberId FROM Payments WHERE NOT EXISTS (SELECT * FROM Payments WHERE [Year] = @year AND [Amount] = @amount)"
;
cmd.Parameters.Clear();
cmd.CommandText = Sql;
cmd.Parameters.AddWithValue(
"@Id"
, tbID.Text);
cmd.Parameters.AddWithValue(
"@year"
, tbYear.Text);
cmd.Parameters.AddWithValue(
"@amount"
, tbAmount.Text);
cmd.ExecuteNonQuery();
MessageBox.Show(
"Data Added"
);
tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();
con.Close();
}
}
}
I have 2 tables "Members" & "Payments" When I enter one new record it records its and then if I enter the same over and over again it duplicates it many times which it "SHOULD NOT" I should get warning that it already exists. some how I am stuck with this can anyone assist !
Reply
Answers (
1
)
Why Use the static class in C#
change text of a label