title: Must declare the scalar variable or is it something else
body: <p>I asked this question previously <a href="https://stackoverflow.com/questions/21311731/must-declare-the-scalar-variable">here</a> and I am not sure if I am having the exact same problem, related problem or different problem but I will ask anyway.</p>
  
  <p>I getting error of </p>
  
  <blockquote>
    <p>Must declare the scalar variable "@ID"</p>
  </blockquote>
  
  <p>I have a global string variable...</p>
  
  <pre><code>string picLoc;
  </code></pre>
  
  <p>I have a insert method that looks something like this...</p>
  
  <pre><code>cmd.CommandText = "INSERT into VolunteersMainDetails(ID , " + 
                    "Name, Photo)" +
  
                    "VALUES(@ID, " + 
                    "@Name, @Photo)";
  
  cmd.Parameters.AddWithValue("@ID", txtID.Text);
  cmd.Parameters.AddWithValue("@Name", txtName.Text);
  
  FileStream fs = new FileStream(@picLoc, FileMode.Open, FileAccess.Read);
  byte[] imgByteArr = new byte[fs.Length];
  fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
  fs.Close();
  
  cmd.Parameters.Add(new SqlParameter("Photo", imgByteArr));
  cmd.Parameters.Clear();
  cmd.Parameters.Add("@Photo", SqlDbType.VarBinary, -1);
  cmd.Parameters["@Photo"].Value = DBNull.Value;
  </code></pre>
  
  <p>I am not sure if the clear parameter is the problem and if it is then if remove that code I will get error of...</p>
  
  <blockquote>
    <p>The variable name '@Photo' has already been declared. Variable names must be unique within a query batch or stored procedure.</p>
  </blockquote>
  
  <p>I believe I have used @Photo three times in the parameters if I don't use clear parameters.</p>
  
  <p>If the clear parameter is not the problem then is the problem with code for photo? or Insert and Value code format? </p>
  
  <p>The following method is to open dialog to find the image...</p>
  
  <pre><code> private void brwsphoto_Click(object sender, RoutedEventArgs e)
      {
          try
          {
              FileDialog fldlg = new OpenFileDialog();
              fldlg.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
              fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
              fldlg.ShowDialog();
              {
                picLoc = fldlg.FileName;
                ImageSourceConverter isc = new ImageSourceConverter();
                image1.SetValue(Image.SourceProperty,isc.ConvertFromString(picLoc));
              }
              fldlg = null;
          }
  
         catch (Exception ex)
         {
          MessageBox.Show(ex.Message.ToString());
         }
      }
  </code></pre>
  
  <p>Thanks in advance if anyone can help me? </p>
  
  <p><strong>Update 1a</strong></p>
  
  <p>@Habib</p>
  
  <p>I have amended the insert method as you have suggested as...</p>
  
  <pre><code>         cmd.Parameters.Add(new SqlParameter("VPhoto", imgByteArr)); // removed this code
           cmd.Parameters.Clear(); // removed this code
  
          cmd.Parameters.Add("@Photo", SqlDbType.VarBinary, -1); // this code included            
          cmd.Parameters["@Photo"].Value = DBNull.Value; // this code included  
  </code></pre>
  
  <p>So now I am getting the following error </p>
  
  <blockquote>
    <p>Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.</p>
  </blockquote>
  
  <p><strong>Solution</strong></p>
  
  <p>Solution is I change data type from varbinary(max) to image. This seems to solve the problem. I do not know how to use varbinary(max) for storing imaging and presently I am not bothered about it either. </p>