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
Israel
698
1.3k
217.7k
Repeated sqlparameter's values not allowed. How to resolv?
Jan 10 2020 8:41 AM
Hi,
How can I write correctly codes that values are repeated in the segond piece of codes? Why I am asking this question? Just because there are some parameters's values underlined (showing that some values are repeated). But its the real column's name.
For example these two pieces codes are into the same button:
First piece:
conn =
new
SqlConnection(
"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\x\\WindowsFormsApplication1\\App_datas\\test.mdf;Integrated Security = True;Integrated Security = True"
);
comm =
new
SqlCommand();
if
(conn.State != ConnectionState.Open)
conn.Open();
SqlCommand comm =
new
SqlCommand();
comm1.Connection = conn;
SqlParameter date =
new
SqlParameter(
"@date"
, SqlDbType.VarChar);
SqlParameter name =
new
SqlParameter(
"@name"
, SqlDbType.VarChar);
comm1.Parameters.Add(date);
comm1.Parameters.Add(name);
date.Value = dtDate.Text;
name.Value = txtName.Text;
comm.Connection = conn;
comm.CommandText =
"insert into accounting ([date],[name])values(@date,@name)"
;
{
if
(MessageBox.Show(
"Are you sure to save?"
,
""
, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Second piece:
// Its underline where its colored in red
conn =
new
SqlConnection(
"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\x\\WindowsFormsApplication1\\App_datas\\test.mdf;Integrated Security = True;Integrated Security = True"
);
comm =
new
SqlCommand();
if
(conn.State != ConnectionState.Open)
conn.Open();
SqlCommand comm =
new
SqlCommand();
comm1.Connection = conn;
SqlParameter date =
new
SqlParameter(
"@date"
, SqlDbType.VarChar);
//here
SqlParameter name =
new
SqlParameter(
"@name"
, SqlDbType.VarChar);
// here
comm1.Parameters.Add(date);
comm1.Parameters.Add(name);
date.Value = dtDate.Text;
name.Value = txtName.Text;
comm.Connection = conn;
comm.CommandText =
"insert into accounting ([date],[name])values(@date,@name)"
;
{
if
(MessageBox.Show(
"Are you sure to save?"
,
""
, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Reply
Answers (
3
)
How to load data in view from controller of button click
# linq query mvc