title: Convert combobox string value to int in C# AND pass value in SQL server DB
body: <p>I hope I can explain clearly my dilemma with programming. I'm fairly new in programming so please bear with me. I am using C# and SQL Server 2014.</p>
  <p>I made a database in SQL server with tables named Roles and AccountRegistration:</p>
  <p>Roles Table has RoleId(pk) and RoleName while
  AccountRegistration table has Username(pk), Password, RoleId(fk), FullName, Address.</p>
  <p>Both tables are related through RoleID</p>
  <p>Now, I created an Account Registration form (C#) with Username, Password, FullName, Address and RoleName.</p>
  <p>RoleName is in a combobox, if user picks Admin or Employee how can this be converted into an int so it can be passed on to the RoleId field?</p>
  <p>Here is what I have tried (using SQL server only), but it doesn't work.</p>
  <pre><code>INSERT INTO AccountRegistration(RoleID)
  SELECT a.RoleName
  FROM Roles a
  INNER JOIN AccountRegistration b ON cast(a.RoleId as nvarchar(255))=b.RoleID
  </code></pre>
  <p>Error msg:</p>
  <blockquote>
  <p>Msg 245, Level 16, State 1, Line 1</p>
  <p>Conversion failed when converting the nvarchar value 'Admin' to data type int.</p>
  </blockquote>
  <p>I haven't tried coding this in C# since I'm still trying to figure out the logic.</p>
  <pre><code>SqlConnection con = new SqlConnection(@&quot;Data Source=***\sqlexpress;Initial Catalog=****;User ID=**;Password=****&quot;);
         
  
          public FrmAddNewAccount()
          {
              InitializeComponent();
          }
  
          private void FrmAddNewAccount_Load(object sender, EventArgs e)
          {
              SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(&quot;Select * From Roles&quot;, con);
              DataSet dataSet = new DataSet();
              sqlDataAdapter.Fill(dataSet);
              cmbRole.DisplayMember = &quot;RoleName&quot;;
              cmbRole.ValueMember = &quot;RoleId&quot;;
              cmbRole.DataSource = dataSet;
          }
  
          private void btnSave_Click(object sender, EventArgs e)
          {
              con.Open();
              SqlDataAdapter sda = new SqlDataAdapter(&quot;Insert into AccountRegistration(Username, Password, RoleID, FullName, Address) VALUES ('&quot; + txtUsername.Text + &quot;', '&quot; + txtPassword.Text + &quot;', '&quot; + cmbRole.Text + &quot;', '&quot; + txtFullName.Text +&quot;', '&quot;+txtAddress.Text+&quot;')&quot;, con);
              sda.SelectCommand.ExecuteNonQuery();
              con.Close();
          }
  
         
          private void cmbRole_SelectedValueChanged(object sender, EventArgs e)
          {
              string RoleId = cmbRole.SelectedValue.ToString();
  
          }
  </code></pre>