hope someone can help with this
I am trying to INSERT a row of data into table [Issues] then immediately get the Guid from the ID column of Issues so it can be inserted into another table Actions, creating a link between the two. The relationship is [Issues]one to many[Actions].
When I run this code it causes a Specified Cast is not valid error here: Guid lastID = (Guid)sel.ExecuteScalar();
lastID is the variable I want to assign the last Guid created to so it can be passed into another table INSERT for table Actions.
If anyone can help that would be great! Thanks
I am using C# 2010 Express
//This is declared at the top of the form
public Guid lastID;
private void pbSaveIssue_Click(object sender, EventArgs e)
{
SqlCeConnection ArcBaseConn = new SqlCeConnection(@"Data Source=|DataDirectory|\ArcBase.sdf");
try
{
string cmdIssueSubmit = "INSERT INTO Issues (Type,Description,Dateraised,Site,Reportedby,Status) VALUES (@Type,@Description,@Dateraised,@Site,@Reportedby,@Status)";
string TypeSubmit = cbIssueType.Text;
string DescriptionSubmit = rtbDescription.Text;
string SiteSubmit = cbIssueSite.Text;
string ReportedbySubmit = cbReportedBy.Text;
string DateraisedSubmit = dtpDateRaised.Text;
SqlCeCommand sel = new SqlCeCommand();
sel.Connection = ArcBaseConn;
sel.CommandType = CommandType.Text;
sel.CommandText = cmdIssueSubmit;
sel.Parameters.AddWithValue("@Type", TypeSubmit);
sel.Parameters.AddWithValue("@Description", DescriptionSubmit);
sel.Parameters.AddWithValue("@Dateraised", DateraisedSubmit);
sel.Parameters.AddWithValue("@Site", SiteSubmit);
sel.Parameters.AddWithValue("@Reportedby", ReportedbySubmit);
sel.Parameters.AddWithValue("@Status", lbStatus.Text);
ArcBaseConn.Open();
sel.ExecuteNonQuery();
//Here is the problem
// Grab the last Unique ID for the Issue just entered and assign to a variable so that it can be entered with the Actions below.
string cmdGetlastID = "SELECT @@IDENTITY";
sel.CommandText=cmdGetlastID;
Guid lastID = (Guid)sel.ExecuteScalar();
ArcBaseConn.Close();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.ExitThread();
}