Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I design desktop Application for Clinic when i add new Patient when i run code
my Error IS

Error InvalidCastException: Invalid cast from 'System.String' to 'System.Byte[]'.

What I have tried:

my Code

function Add :

C#
public static class Add_New_Patient
    {

        public static void Add_Patient(int id, string Patient_Identity, string Patient_Name, string Age, string Gender, string Phone, string Patient_State, string Patient_Date,
            string Patient_Email, float Cost, float Paid, float Remain,  byte[] Patient_img, string NOTES


            )
        {
            DXL.DXL ob = new DXL.DXL();
            ob.open();

            SqlParameter[] p = new SqlParameter[14];
            p[0] = new SqlParameter("@id", SqlDbType.Int);
            p[0].Value = id;

            p[1] = new SqlParameter("@Patient_Identity", SqlDbType.NVarChar,50);
            p[1].Value = Patient_Identity;

            p[2] = new SqlParameter("@Patient_Name", SqlDbType.NVarChar, 50);
            p[2].Value = Patient_Name;

            p[3] = new SqlParameter("@Age", SqlDbType.NVarChar, 50);
            p[3].Value = Age;


            p[4] = new SqlParameter("@Gender", SqlDbType.NVarChar, 50);
            p[4].Value = Gender;


            p[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 50);
            p[5].Value = Phone;

            p[6] = new SqlParameter("@Patient_State", SqlDbType.NVarChar, 50);
            p[6].Value = Patient_State;


            p[7] = new SqlParameter("@Patient_Date", SqlDbType.Date);
            p[7].Value =Patient_Date;

            p[8] = new SqlParameter("@Patient_Email", SqlDbType.NVarChar, 50);
            p[8].Value = Patient_Email;

            p[9] = new SqlParameter("@Cost", SqlDbType.Float);
            p[9].Value = Cost;

            p[10] = new SqlParameter("@Paid", SqlDbType.Float);
            p[10].Value = Paid;

            p[11] = new SqlParameter("@Remain", SqlDbType.Float);
            p[11].Value = Remain;


            p[12] = new SqlParameter("@Patient_img", SqlDbType.Image);
            p[12].Value = Patient_Email;

            p[13] = new SqlParameter("@NOTES", SqlDbType.NVarChar, 50);
            p[13].Value = NOTES;


            ob.RUA("New_Patient", p);
            ob.close();


        }


My Class Connection Is





C#
public class DXL
    {


        SqlCommand cmd;
        SqlDataAdapter da;
        SqlConnection cn;
        DataTable dt = new DataTable();

        public DXL()  /*  باني افتراضي لاجل بناء اتصال */
        {
            cn = new SqlConnection(@"server=DESKTOP-VTLRB7T;database=DataBase_Clinic ;integrated security=true");
        }

        public void open()
        {
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
        }

        public void close()
        {
            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
            }
        }
        //تابع لقراءة البيانات من قاعدة البيانات
        public DataTable Reader(string sp, SqlParameter[] p)
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sp;
            cmd.Connection = cn;
            if (p != null)
            {
                cmd.Parameters.AddRange(p);
            }
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            return dt;


        }
        //Remove ,Update ,Add
        public void RUA(string sp, SqlParameter[] p)
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sp;
            cmd.Connection = cn;
            if (p != null)
            {
                cmd.Parameters.AddRange(p);
            }
            cmd.ExecuteNonQuery();

        }
Posted
Updated 17-Feb-22 6:55am
v2

1 solution

p[12] = new SqlParameter("@Patient_img", SqlDbType.Image);
p[12].Value = Patient_Email;
 
Share this answer
 
Comments
ahmedbelal 18-Feb-22 4:18am    
Thanks PIEBALDconsult

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