Reputation: 401
I have two tables which is appointment and medicalcentre which have relationship with each other using mcID. For my appointment form, i populate the dropdownlist to display the McCentre field of medicalcentre table. And what I want to do is to, based on what the user select for the McCentre in the dropdownlist, and submit the form, the appropiate mcID of MedicalCentre table will be inserted into the appointment table. etc, If I select Sliver Cross Family Clinic and submit the form, the appointment table will insert a new record of mcID 4. Because based on medicalcentre table, sliver cross family clinic belongs to mcID 4.
For the below codes that i posted. After choosing mcCentre (Hwang & Liang Family Clinic) and clicking sign up button, I got (Conversion failed when converting the nvarchar value 'Hwang & Liang Family Clinic' to data type int.) Help?
APPOINTMENT FORM
APPOINTMENT AND MEDICALCENTRE TABLES
Error following hutchonoid code
Error following hutchonoid code 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Data;
public partial class appointment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
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
}
}
protected void btnCreate_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
{
try
{
int ID = Convert.ToInt32(Session["ID"].ToString());
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", ID);
cmd.Parameters.AddWithValue("@mcID", Convert.ToInt16(ddlMedicalCentre.SelectedValue.Trim()));
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: 1
Views: 1815
Reputation: 33306
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
Upvotes: 1