I am inserting a new states in the table from dropdown list , the dropdown list has new in the list, clicking on new gets me a pop up which takes the new state and inserts in a table. My requirement is I want to get the last inserted state in the dropdownlist and its id to be selected as soon as insertion is done.
The second function is to insert the contact details where i have countryid,stateid,cityid
Please give me a solution
protected void stateSave(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("newStates", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter outparm = new SqlParameter("@stateID", SqlDbType.Int);
outparm.Direction = ParameterDirection.Output;
cmd.Parameters.Add(outparm);
cmd.Parameters.AddWithValue("@statename", state.Value);
cmd.Parameters.AddWithValue("@countryID", countrydropdown.SelectedValue);
conn.Open();
cmd.ExecuteNonQuery();
stateID = Convert.ToInt32(outparm.Value);
Panel2.Visible = true;
Panel1.Visible = false;
}
}
protected void submitDetails_click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(cs))
{
cmd = new SqlCommand("insertContact", conn); /* executes insertContact procedure and insert records
into Contact table.This is the first procedure which executes
after the user hits submit button */
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter outparm = new SqlParameter("@contactID", SqlDbType.Int);
outparm.Direction = ParameterDirection.Output; /* specifies the type of a parameter within a query */
cmd.Parameters.Add(outparm);
cmd.Parameters.AddWithValue("@fname", fname.Value);
cmd.Parameters.AddWithValue("@mname", mname.Value);
cmd.Parameters.AddWithValue("@lname", lname.Value);
cmd.Parameters.AddWithValue("@countryID", countrydropdown.SelectedValue);
cmd.Parameters.AddWithValue("@stateID", statedropdown.SelectedValue);
cmd.Parameters.AddWithValue("@cityID", citydropdown.SelectedValue);
cmd.Parameters.AddWithValue("@address1", Address1.Value);
cmd.Parameters.AddWithValue("@address2", Address2.Value);
cmd.Parameters.AddWithValue("@pincode", Pincode.Value);
cmd.Parameters.AddWithValue("@mobile1", Mobile1.Value);
cmd.Parameters.AddWithValue("@mobile2", Mobile2.Value);
cmd.Parameters.AddWithValue("@phone", Phone.Value);
cmd.Parameters.AddWithValue("@email1", Email1.Value);
cmd.Parameters.AddWithValue("@email2", Email2.Value);
cmd.Parameters.AddWithValue("@email3", Email3.Value);
conn.Open();
cmd.ExecuteNonQuery();