You need to convert the selected value from a string
to an Int
.
This returns a string:
ddlMedicalCentre.SelectedValue.Trim()
And the statement is expecting an Int
.
This will fix the issue:
cmd.Parameters.AddWithValue("@mcID", Convert.ToInt16(ddlMedicalCentre.SelectedValue.Trim()));
Update
You need to make sure that your dropdown list is using the Id as the value like this:
ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcId"].ToString();
and not this which is setting the text to the value:
ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcCentre"].ToString();
You should leave DataTextField as is though to display the name.
Update 2
Copy this into a new method i.e.
private void BindMedicalDropdowns()
{
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
SqlCommand com = new SqlCommand("select mcCentre 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.DataTextField = ds.Tables[0].Columns["mcCentre"].ToString(); // text field name of table dispalyed in dropdown
ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcCentre"].ToString(); // to retrive specific textfield name
ddlMedicalCentre.DataSource = ds.Tables[0]; //assigning datasource to the dropdownlist
ddlMedicalCentre.DataBind(); //binding dropdownlist
}
Then move it out of the !IsPostBack clause so it gets re-bound on postback too.
protected void Page_Load(object sender, EventArgs e)
{
BindMedicalDropdowns();
if (!Page.IsPostBack)
{
}
}
Update 3
On line 32, your select statement should be this, it's missing the mcID
field:
select mcId, mcCentre from MEDICALCENTRE