Repeated sqlparameter's values not allowed. How to resolv?

Israel

Active member
Joined
Jan 10, 2020
Messages
26
Programming Experience
Beginner
Hi,

How can I write correctly codes that values are repeated in the second 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:

C#:
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


C#:
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();
        }
 
Last edited by a moderator:
You can either reuse the existing SqlParameter objects and add them to different command object, or you can reuse the existing variables and just assign new SqlParameter objects to them.
When you do this you declare a new variable and assign something to it:
C#:
SqlParameter date = value/object
or
var date = value/object
When you do this you assign something to an existing variable:
C#:
date = value/object
 
Your code doesn't make sense. You are assigning a new object to the comm variable, then you declare a comm variable and assign another new object to it. You are also referring to both comm and comm1 in both code snippets. Most telling though, is that both snippets are exactly the same. You might want to try again and start with code that makes sense. You have bigger problems than parameters there.
 

Latest posts

Back
Top Bottom