Pony
Pony

Reputation: 401

Value is always 1 in the database, whenever I choose any option in the dropdownlist

I have two table, called MEDICALCENTRE AND APPOINTMENT. For medicalcenter table, primary key is mcID and appointment table primary key is appointmentID. Both have this mcID field. I make the drop down list populate data from MEDICALCENTRE table, which have data inside which is 1, 3 and 4. After choosing the mcID I want, I click submit and it will get sent to appointment table mcID. The problem is Whether I choose 1, 3 or 4 in the drop down list, the value is always 1 on the mcID field, in the appointment table.

  protected void Page_Load(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ToString(); // connection string
        SqlConnection con = new SqlConnection(constr);
        con.Open();

    SqlCommand com = new SqlCommand("select * from MEDICALCENTRE", con); // table name 
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    da.Fill(ds);  // fill dataset
    ddlMedicalCentre.DataTextField = ds.Tables[0].Columns["mcID"].ToString(); // text field name of table dispalyed in dropdown
    ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcID"].ToString();             // to retrive specific  textfield name 
    ddlMedicalCentre.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
    ddlMedicalCentre.DataBind();  //binding dropdownlist
}



protected void btnCreate_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                Guid guid;
                guid = Guid.NewGuid();
                String strStatus = "waiting";
                string sql = "INSERT INTO appointment (aStatus,aDate, aTime, aContact, aHeight, aWeight, patientID, mcID)";
                sql += "VALUES (@aStatus, @aDate, @aTime, @aContact, @aHeight, @aWeight, @patientID, @mcID)";
                cmd.Parameters.AddWithValue("@aStatus", strStatus);
                cmd.Parameters.AddWithValue("@aDate", txtDate.Value);
                cmd.Parameters.AddWithValue("@aTime", txtTime.Value);
                cmd.Parameters.AddWithValue("@aContact", txtContact.Value.Trim());
                cmd.Parameters.AddWithValue("@aHeight", txtHeight.Value.Trim());
                cmd.Parameters.AddWithValue("@aWeight", txtWeight.Value.Trim());
                cmd.Parameters.AddWithValue("@patientID", txtpatientID.Value.Trim());
                cmd.Parameters.AddWithValue("@mcID", ddlMedicalCentre.SelectedValue);

                cmd.Connection = con;
                cmd.CommandText = sql;
                con.Open();

                cmd.ExecuteNonQuery();

               // Session.Add("Username", txtFirstName.Value);
              //  Session.Add("Password", txtContact.Value);
              //  FormsAuthentication.SetAuthCookie(txtFirstName.Value, true);
                Response.Redirect("../index.aspx");

            }

            finally
            {
                con.Close();
            }


        }
    }
}

Upvotes: 0

Views: 42

Answers (1)

Steve
Steve

Reputation: 216303

In your Page_Load method you don't check for the IsPostBack property, so your method is executed at every postback resetting the DropDown list to the first item

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ToString(); 
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        ......
    }
}

Upvotes: 2

Related Questions