Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
What it wrong with this function ?
Jahangir Khan
May 16 2016 6:45 AM
353
Reply
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 !
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Why Use the static class in C#
change text of a label