private void AddExit_Click(object sender, EventArgs e) { publisherloc pl = new publisherloc(); pl.Name = textBox1.Text; pl.Street = textBox2.Text; pl.City = textBox3.Text; pl.State = textBox4.Text; pl.Pin = Convert.ToInt32(textBox5.Text); pl.Country = textBox6.Text; /*{//Getting the ID of the location record last entered. This will go as foreign key in Publisher's location column. SqlCeDataAdapter adap = new SqlCeDataAdapter("SELECT max(ID) FROM Location", "Data Source=D:\\TSLU\\MyDatabase4.sdf"); DataSet dat = new DataSet(); adap.Fill(dat); Int32 First = Convert.ToInt32(dat.Tables[0].Rows[0]["Column1"].ToString()); pl.location = First; }*/ //Finally calling the method pl.location = 0; pl.publoc(pl); Close(); }
public void publoc(publisherloc pl) { SqlCeConnection Conn = new SqlCeConnection("Data Source=D:\\TSLU\\MyDatabase4.sdf"); Conn.Open(); SqlCeCommand Cmd = new SqlCeCommand(); Cmd.Connection = Conn; Cmd.CommandText = "Insert into Location (Street,City,State,Country,PIN)Values(@Street,@City,@State,@Country,@PIN)"; Cmd.Parameters.AddWithValue("@Street", pl.Street); Cmd.Parameters.AddWithValue("@City", pl.City); Cmd.Parameters.AddWithValue("@State", pl.State); Cmd.Parameters.AddWithValue("@Pin", pl.Pin); Cmd.Parameters.AddWithValue("@Country", pl.Country); Cmd.ExecuteNonQuery(); //Conn.Close(); {//Getting the ID of the location record last entered. This will go as foreign key in Publisher's location column. SqlCeDataAdapter adap = new SqlCeDataAdapter("SELECT max(ID) FROM Location", "Data Source=D:\\TSLU\\MyDatabase4.sdf"); DataSet dat = new DataSet(); adap.Fill(dat); Int32 First = Convert.ToInt32(dat.Tables[0].Rows[0]["Column1"].ToString()); pl.location = First; } //Conn.Open(); //SqlCeCommand Cmd2 = new SqlCeCommand(); Cmd.CommandText = "Insert into Publisher (Name, Location)Values(@Name,@Location)"; Cmd.Parameters.AddWithValue("@Name", pl.Name); Cmd.Parameters.AddWithValue("@Location", pl.location); Cmd.ExecuteNonQuery(); Conn.Close(); }