1

How can I insert data from a text field into a foreign key in the database. Basically, I have tables named Employee and Role, RoleId is a foreign key in Employee, but the role text field is a string. I have this

String Query = "insert into EMPLOYEE (LastName,FirstName,Manager,RoleId,HireStatus) values('" + this.txtLName.Text + "','" + this.txtFName.Text + "','" + this.txtManager.Text + "','"  + this.txtRoleId.Text + "','" + this.txtHireStatus.Text + "');";

Edited - I have this, but I cannot have select with the following string

string Query = "insert into EMPLOYEE(LastName,FirstName,RoleId,Manager,HireStatus) values('" + this.txtLName.Text + "','" + this.txtFName.Text + "','" + ( SELECT RoleId From Role WHERE Role.RoleId = this.txtRole.Text ) +  "','" + this.txtManager.Text +  "','" + this.cmbHireStatus.Text + "');";
gre_gor
  • 5,546
  • 9
  • 35
  • 41

2 Answers2

0

I think you should do this:

String Query = "insert into EMPLOYEE (LastName,FirstName,Manager,RoleId,HireStatus) values('" + this.txtLName.Text + "','" + this.txtFName.Text + "','" + this.txtManager.Text + "','" + this.txtRoleId.Text + "','" + this.txtHireStatus.Text + "');";
gre_gor
  • 5,546
  • 9
  • 35
  • 41
0

If you are trying to add a new Employee assigned to an existing role, then a better design would be have a dropdown with all Roles populated instead of a textbox for user to enter.

The dropdown should get filled with all the Roles from Roles table. (map the SelectedValue attribute of the dropdown to - RoleId and SelectedText to RoleName).

When you submit the form, you will get the SelectedValue (RoleId) which you can directly send as part of the INSERT statement, i.e., dropDownRole.SelectedValue instead of txtRoleId.Text

Note: Your insert query seems to be a classic candidate for SQL Injection. I suggest you transform it to Parameterized query.

Update: Now that I came to know it is a Winforms application, adding more detailed snippets. Here is how you can do it in the Win Forms world (not as intuitive as web app world though :) ) and please bear with me, it has been years since I had written a winforms snippet.

Define your dropdown combo box like this -

cbxRole.DataSource = roles; // data from back end
cbxRole.Name = "Role";
cbxRole.DropDownStyle = ComboBoxStyle.DropDownList;
cbxRole.DisplayMember = "RoleName"; // should match the property name in your roles data table
cbxRole.ValueMember = "RoleId"; // should match the property name in your roles data table
cbxRole.SelectedItem = null;
cbxRole.SelectedText = "Select Role";

Observe the ValueMember and DisplayMember properties. DisplayMember tells what property in the roles data source to be used to display as the dropdown item text. ValueMember specifies the property to be used to identify each of the item behind the scenes.

When you submit the details, access the SelectedValue property to get the RoleId corresponding to the selected role name.

private void btnCreate_Click(object sender, EventArgs e)
{
    var firstName = txtFirstName.Text;
    var lastName = txtLastName.Text;
    var roleId = (int)cbxRole.SelectedValue;
}

The SelectedValue property fetches the value of the column identified by the ValueMember property in the dropdown definition, which is the RoleId value. Now, you can send this 'roleId' variable's value to the insert query.

here is how it looks like when you submit - enter image description here

here is quick sample UI - enter image description here

Thimmu Lanka
  • 412
  • 3
  • 12
  • Thank you, this is what I had in mind, but don't really know how to go about it after having a dropdown populated with the roles. How do I identify what role the employee has In the employee table since my RoleId is an int in the employee table – Hello world Jun 09 '20 at 03:54
  • Edited - I have this, but I cannot have select with the following string string Query = "insert into EMPLOYEE(LastName,FirstName,RoleId,Manager,HireStatus) values('" + this.txtLName.Text + "','" + this.txtFName.Text + "','" + ( SELECT RoleId From Role WHERE Role.RoleId = this.txtRole.Text ) + "','" + this.txtManager.Text + "','" + this.cmbHireStatus.Text + "');"; – Hello world Jun 09 '20 at 04:06
  • If you have a drop down on the UI, you do not have to do the `SELECT RoleId...`. Because the Dropdown's SelectedValue property will have the RoleId which you can directly pass to the query just as I mentioned in my third paragraph above. – Thimmu Lanka Jun 09 '20 at 04:09
  • What is your UI, is it ASP.NET or something else? Depending on that I can add the dropdown sample to my answer – Thimmu Lanka Jun 09 '20 at 04:10
  • it is windows form app. I added the drop down menu and populate it with my role from the Role table, but it is still saying cannot convert string to int. What do you advice i add in the insert string – Hello world Jun 09 '20 at 05:32
  • Edited the answer and added more specific snippets. HTH. – Thimmu Lanka Jun 10 '20 at 06:25