Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi! i have write this code but when i enter save button for to save data in data base this error come"Incorrect syntax near '12'." .How can i handle this error.
C#
Namespace HOSPITAL_MANAGEMENT_SYSTEM.Forms
{
    public partial class Doctor_Page : DevExpress.XtraEditors.XtraForm
    {
        public Doctor_Page()
        {
            InitializeComponent();
        }
        HOSPITAL_MANAGEMENT_SYSTEM.dataaccess dbcs = new dataaccess();

        private void Doctor_Page_Load(object sender, EventArgs e)
        {

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            DateTime datee = DateTime.Parse(date.EditValue.ToString());
            //DateTime dateee = 
            string starttime = txtbx_starttime.Text.Trim();
            string endtime = txtbx_endtime.Text.Trim();
            string patienttype = combo_patnttype.SelectedIndex.ToString();
            string patientfee = txtbx_patntfee.Text.Trim();
            string patientname = txtbx_patntname.Text.Trim();
            string phonnum = txtbx_phnnum.Text.Trim();
            string appoint = combo_appointtype.SelectedIndex.ToString();
            string insert = string.Format("insert into doctor values({0},'{1}','{2}','{3}',{4},'{5}',{6},'{7}')", datee, starttime, endtime,patienttype,patientfee,patientname, phonnum, appoint);
            SqlCommand sqlcm = new SqlCommand (insert, dbcs.open());
            sqlcm.ExecuteNonQuery();
            

        }
Posted
Updated 30-Apr-15 8:33am
v2
Comments
Tomas Takac 30-Apr-15 14:35pm    
If I have to guess then you are missing apostrophes around the first value being inserted - it should be '{0}'.
Atta Ur Rahman 118 30-Apr-15 15:37pm    
Thank you sir!
Atta Ur Rahman 118 30-Apr-15 14:49pm    
Thank you sir!
Atta Ur Rahman 118 30-Apr-15 15:01pm    
Sir another question is this that in this form i have two comboboxes. Both have two values. 1st is for patient type and another is for appointment type. In pat... type item are New and existing and apoint type has Follow up and new problem. I say that when i select in the first comboobx "New" the another combobox disable automatically.
Karthik_Mahalingam 30-Apr-15 15:15pm    
Please reply to the concern person to get this notified, else he/she will not get the mail/notification.

1 solution

You want to use parameters so that you don't have sql injection security issues.

For example:
C#
string insert = "INSERT INTO doctor VALUES (@field1, @field2..."
sqlcm.Parameters.AddWithValue("@field1", txtField1.Text);
...


That will also solve your syntax issue.
 
Share this answer
 
v2
Comments
Tomas Takac 30-Apr-15 14:45pm    
+5 yes, that's the way to go
Maciej Los 30-Apr-15 15:11pm    
Short and to the point!
Sergey Alexandrovich Kryukov 30-Apr-15 15:25pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900