I have been having issues getting my insert to work. I have been considering it to be done based on the selectedindex of two comboboxes(1. cboCustomer 2. cboProduct) and one textbox (RegistrationDate). The error I get the most would be related to the CustomerID being Null; which I thought was supposed to be automatically generated. Of course what confuses me the most are the form specifications. The cboCustomer combobox has to display the Name of the customer; which isn't part of the Registration table. And the cboProduct combobox has to display the product name; which also isn't related to the Registration table. The registration table is where I need to make the insert, but the combobox are loaded using two methods that fetch the name from the Customer and Product tables, which cannot be used for the insert. Which, if I try to Insert just the registrationDate I get an error saying ProductCode cannot be Null. I am also a bit confused if the AddRegistration method will run the insert despite returning a boolean value. Thank you for any assistance and for your time.
Most of the code below has been changed a few times, because I have tried several solutions with no success.
This is the code that I have to fetch the values in to be passed to the AddRegistration method.
public void PutRegistrationData(Registration registration)
{
registration.RegistrationDate = registrationDateTextBox.Text;
registration.ProductCode = productComboBox.SelectedValue.ToString();
}
This is the code for the Button on the form that triggers the insert in the AddRegistration.
private void btnRegister_Click(object sender, EventArgs e)
{
if (this.IsPresent(registrationDateTextBox.Text))
{
registration = new Registration();
this.PutRegistrationData(registration);
try
{
RegistrationDB.AddRegistration(registration);
MessageBox.Show("The product was entered successfully", "Registration Success");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
}
}
This is the code I have the for my insert within AddRegistration of the RegistrationDB Class.
public static bool AddRegistration(Registration registration)
{
try
{
SqlConnection connection = TechSupportDB.GetConnection();
string insertStatement = "INSERT INTO Registrations (ProductCode, RegistrationDate" +
"VALUES (@ProductCode,@RegistrationDate)";
SqlCommand insertCommand = new SqlCommand(insertStatement, connection);
//insertCommand.Parameters.AddWithValue("@CustomerID", registration.CustomerID);
insertCommand.Parameters.AddWithValue("@ProductCode", registration.ProductCode);
insertCommand.Parameters.AddWithValue("@RegistrationDate", registration.RegistrationDate);
insertCommand.Connection = connection;
connection.Open();
insertCommand.ExecuteNonQuery();
connection.Close();
return true;
}
catch
{
return false;
}
}
}
}