0

Alright I am new to T-SQL and I am trying to get my insert method to work. The error I get is unknown constructor at my insert method. I am not sure why I have the error, I am sure I sure I haven't referenced something correctly. Thank you before hand!

SqlConnection dbConn = null;
LabelData LadelList = new LabelData();

try
{
   using (dbConn = new SqlConnection(Properties.Settings.Default["connectionname"].ToString()))

       LabelData addNewVersion = new LabelData(@"INSERT INTO PackLabelVersion (VersionID, VersionNumber, FormatID) VALUES (@VersionID, @VersionNumber, @FormatID)", dbConn);
       addNewVersion.Parameters.AddWithValue("@VersionID", VersionID);
       addNewVersion.Parameters.AddWithValue("@VersionNumber", VersionNumber);
       addNewVersion.Parameters.AddWithValue("@Quantity", FormatID);

       dbConn.Open();
       addNewVersion.ExecuteNonQuery();
}
catch (Exception ex)
{
   throw ex;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    The answer from @Habib is correct. I'd like to note though something that's a bit unrelated to the question. You are making a classic mistake in your exception handling. You should avoid catching all exceptions, and most especially avoid (this is a real annoyance to me when I see coworkers do it) throwing the same exception again. If you want to do that, just use "throw;" not "throw ex;". The way you do it, it eats the stack trace. See this post http://stackoverflow.com/a/730255/1246574. – Jim Nov 06 '13 at 21:30

3 Answers3

5

You don't need LabelData, instead it should be SqlCommand.

SqlCommand addNewVersion = new SqlCommand (@"INSERT INTO PackLabelVersion (VersionID,VersionNumber,FormatID) VALUES (@VersionID,@VersionNumber,@FormatID)", dbConn);

also you need to define scope of using statement, currently it is just considering a single statement below it.

Habib
  • 219,104
  • 29
  • 407
  • 436
3

Your using statement does not have braces surrounding the database connection. Therefore it is disposed of right away.

SqlConnection dbConn = null;
LabelData LadelList = new LabelData();
try
{
    using (dbConn = new SqlConnection(Properties.Settings.Default["connectionname"].ToString()))
    {

        SqlCommand addNewVersion = new SqlCommand(@"INSERT INTO PackLabelVersion (VersionID,VersionNumber,FormatID) VALUES (@VersionID,@VersionNumber,@FormatID)", dbConn);
        addNewVersion.Parameters.AddWithValue("@VersionID", VersionID);
        addNewVersion.Parameters.AddWithValue("@VersionNumber", VersionNumber);
        addNewVersion.Parameters.AddWithValue("@Quantity", FormatID);
        dbConn.Open();
        addNewVersion.ExecuteNonQuery();
    }
}
catch (Exception ex)
{
    throw ex;
}

Edit.. Plus you need the SqlCommand not LabelData. (as per Habib)

Nico
  • 12,493
  • 5
  • 42
  • 62
0

You have syntax errors, missing SqlCommand, and the insert values are not set. (vicious cycle)

After declaring your sql connetion

SqlConnection cnn = new SqlConnection(" enter your connection string here ");

write this into the code block below.. it should work then

     cnn.Open();
     SqlCommand cmd = new SqlCommand("INSERT INTO PackLabelVersion (VersionID,VersionNumber,FormatID) VALUES (@VersionID,@VersionNumber,@FormatID)", cnn);
     cmd.Parameters.AddWithValue("@VersionID", TextBox1.Text);
     cmd.Parameters.AddWithValue("@VersionNumber", TextBox2.Text);
     cmd.Parameters.AddWithValue("@FormatID", TextBox3.Text);
     cmd.ExecuteNonQuery();
     cnn.Close();
Hasan Alaca
  • 232
  • 2
  • 14