سلام ممنونم مشکل رو پیدا کردم مشکل اصلی من از Query بود که در SelectedIndexChanged گذاشته بودم ولی وقتی کد های زیر رو قرار میدم خیلی کند میشه راهی وجود داره یا خیر
string commandText1 = "select SUM(isnull(opd,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt1 = new SqlCommand(commandText1, db.con); db.con.Open(); lblopd.Text = dt1.ExecuteScalar().ToString(); db.con.Close(); string commandText2 = "select SUM(isnull(lab,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt2 = new SqlCommand(commandText2, db.con); db.con.Open(); lbllab.Text = dt2.ExecuteScalar().ToString(); db.con.Close(); string commandText3 = "select SUM(isnull(endoc,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt3 = new SqlCommand(commandText3, db.con); db.con.Open(); lblendos.Text = dt3.ExecuteScalar().ToString(); db.con.Close(); string commandText4 = "select SUM(isnull(ultr,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt4 = new SqlCommand(commandText4, db.con); db.con.Open(); lblultra.Text = dt4.ExecuteScalar().ToString(); db.con.Close(); string commandText5 = "select SUM(isnull(dnc,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt5 = new SqlCommand(commandText5, db.con); db.con.Open(); lbldnc.Text = dt5.ExecuteScalar().ToString(); db.con.Close(); string commandText6 = "select SUM(isnull(gyn,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt6 = new SqlCommand(commandText6, db.con); db.con.Open(); lblgyn.Text = dt6.ExecuteScalar().ToString(); db.con.Close(); string commandText7 = "select SUM(isnull(dliver,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt7 = new SqlCommand(commandText7, db.con); db.con.Open(); lbldeliver.Text = dt7.ExecuteScalar().ToString(); db.con.Close(); string commandText8 = "select SUM(isnull(orth,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt8 = new SqlCommand(commandText8, db.con); db.con.Open(); lblortho.Text = dt8.ExecuteScalar().ToString(); db.con.Close(); string commandText9 = "select SUM(isnull(ambu,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt9 = new SqlCommand(commandText9, db.con); db.con.Open(); lblambu.Text = dt9.ExecuteScalar().ToString(); db.con.Close(); string commandText10 = "select SUM(isnull(echo,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt10 = new SqlCommand(commandText10, db.con); db.con.Open(); lblecho.Text = dt10.ExecuteScalar().ToString(); db.con.Close(); string commandText11 = "select SUM(isnull(xrdg,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt11 = new SqlCommand(commandText11, db.con); db.con.Open(); lblxrydigi.Text = dt11.ExecuteScalar().ToString(); db.con.Close(); string commandText12 = "select SUM(isnull(xrs,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt12 = new SqlCommand(commandText12, db.con); db.con.Open(); lblxrys.Text = dt12.ExecuteScalar().ToString(); db.con.Close(); string commandText13 = "select SUM(isnull(xrd,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt13 = new SqlCommand(commandText13, db.con); db.con.Open(); lblxryd.Text = dt13.ExecuteScalar().ToString(); db.con.Close(); string commandText14 = "select SUM(isnull(emg,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt14 = new SqlCommand(commandText14, db.con); db.con.Open(); lblemerg.Text = dt14.ExecuteScalar().ToString(); db.con.Close(); string commandText15 = "select SUM(isnull(ecg,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt15 = new SqlCommand(commandText15, db.con); db.con.Open(); lblecg.Text = dt15.ExecuteScalar().ToString(); db.con.Close(); string commandText16 = "select SUM(isnull(bed,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt16 = new SqlCommand(commandText16, db.con); db.con.Open(); lblbed.Text = dt16.ExecuteScalar().ToString(); db.con.Close(); string commandText17 = "select SUM(isnull(ot,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt17 = new SqlCommand(commandText17, db.con); db.con.Open(); lblot.Text = dt17.ExecuteScalar().ToString(); db.con.Close(); string commandText18 = "select SUM(isnull(pthrap,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt18 = new SqlCommand(commandText18, db.con); db.con.Open(); lblptherap.Text = dt18.ExecuteScalar().ToString(); db.con.Close(); string commandText19 = "select SUM(isnull(phar,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt19 = new SqlCommand(commandText19, db.con); db.con.Open(); lblpharmacy.Text = dt19.ExecuteScalar().ToString(); db.con.Close(); string commandText20 = "select SUM(isnull(dental,0)) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt20 = new SqlCommand(commandText20, db.con); db.con.Open(); lbldental.Text = dt20.ExecuteScalar().ToString(); db.con.Close(); string commandText21 = "select SUM(opd+lab+endoc+ultr+dnc+gyn+dliver+orth+ambu+echo+xrdg+xrs+xrd+emg+ecg+bed+ot+pthrap+phar+dental) as total from pfee where PID = '" + lblpid.Text + "'"; SqlCommand dt21 = new SqlCommand(commandText21, db.con); db.con.Open(); lbltotal.Text = dt21.ExecuteScalar().ToString(); db.con.Close();
با سلام خدمت دوست گلم
مشکل کد شما این است که اولا از کد های تکراری استفاده کردی
دوم اگه توی کدت توجه کنی نزدیک 20 بار db.con.Open(); و db.con.Close(); زدید
یعنی مثل این میمونه 20 توپ میخواهی ببری توی خونتون و هر بار که یک توپ رو میبری داخل در رو هی باز و بسته میکنی
یک جور کدت رو بنویس که یک بار db.con.Open(); در اول بزنی و یک db.con.Close(); در اخر و سعی کن از کد های تکراری کمتر استفاده کنی
بعدش هم یک چیزی داریم به نام برنامه نویسی موازی ( Parallel Programming ) که میتونی همزمان چند کار رو باهم انجام بدی توی سایت تاپ لرن دوره اش هست
موفق و پیروز باشید
سلام چه زبان برنامه نویسی هست؟
اگه c# هست کل پروژه ات را بفرست یک سعی در درست کردنش بکنم بانک اطلاعاتی ات هم بفرست
یک توضیحی هم بده این کدت خروجی چی میده و چیکار میخواهد بکند و یک سوال چرا از حلقه ها استفاده نکردی که 120 خط کد بشه 10 خط یعنی اگه اطلاعات 1000 نفر رو میخواستی باید 1000 تا 4 خط مینوشتی؟!!
و سوال دوم اگه c# هست چرا از linq استفاده نکردی
اگه بلد نیستی اینم اموزش linq https://toplearn.com/downloads/course/TopLearn.Com_CsharpAdvanced_02.rar رایگان هست اینو ببین بعدش کدت رو از نو بنویس
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)