//This is the insertion code
private void button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Andrej\Desktop\B9PROBA\B9\B9\Database5.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand com = new SqlCommand("INSERT INTO ARB5Stadion(StadionID,Naziv,Adresa,Kapacitet,BrojUlaza,GradID ) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + comboBox1.ValueMember + "')", conn);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
MessageBox.Show("Uspesno Uneti Podaci");
}
catch (Exception e1) { MessageBox.Show(e1.Message); }
finally { conn.Close(); }
//And this is the code for the combobox, which reads from one datatable ,using the the id value as an value member and the name as a display memeber.Im essentialy trying to insert the GradID primary key of the ARB5Grad Table into StadionID where GradID is a foreign key
private void Stadion_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Andrej\Desktop\B9PROBA\B9\B9\Database5.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
SqlCommand sc = new SqlCommand("SELECT GradID,Grad FROM ARB5Grad ", conn);
SqlDataReader reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("GradID", typeof(int));
dt.Columns.Add("Grad", typeof(string));
dt.Load(reader);
comboBox1.ValueMember = "GradID";
comboBox1.DisplayMember = "Grad";
comboBox1.DataSource = dt;