Reputation: 65
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;
}
Upvotes: 0
Views: 79
Reputation: 232
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();
Upvotes: 0
Reputation: 12683
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)
Upvotes: 3
Reputation: 223392
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.
Upvotes: 5