ورود

View Full Version : سوال: مشکل در کد delete



asiehmohammadian
دوشنبه 18 اردیبهشت 1391, 19:37 عصر
سلام
من یک گریدویو دارم در انتهای هر سطرگرید یک دکمه گذاشتم به نام انتخاب که مدیر باکلیک روی اون وارد صفحه دیگه ای میشه که میتونه تمام اطلاعات اون سطر انتخابی و به طور کامل ببینه
دراین قسمت تعدادی از فیلدها که فقط برای نمایش به مدیر است به صورت read only است وبعضی از فیلدها توسط مدیر قابل ویرایش است
درانتهای این صفحه یک دکمه بروزرسانی ویک دکمه حذف گذاشتم ، کدی که برای دکمه حذف نوشتم درست کارنمیکنه میشه لطف کنید ببینید عیبش چیه؟
این کدمه:
SqlDataAdapter da = new SqlDataAdapter("delete from t_register where re_fname='" + txtname.Text + "'and re_lname='" + txtlname.Text + "'and re_username='" + txtusername.Text + "'and re_pass='" + txtpass.Text + "' and re_companyname='" + txtcompanyname.Text + "' and re_type='" + txttype.Text + "' and re_typeofproperty='" + txtproperty.Text + "' and re_economiccode='" + txtcode.Text + "' and re_province='" + txtprovince.Text + "' and re_city='" + txtcity.Text + "' and re_website='" + txtweb.Text + "' and re_mobile='" + txtmobile.Text + "' and re_tell='" + txttell.Text + "' and re_fax='" + txtfax.Text + "' and re_email='" + txtemail.Text + "' and re_maritalstatus='" + txtmarital.Text + "' and re_sex='" + txtsex.Text + "' and re_birth='" + txtbirth.Text + "' and re_marriage='" + txtmarriage.Text + "' and re_degree='" + txtdegree.Text + "' and re_lists='" + txtlists.Text + "' and re_color='" + txtcolor.Text + "' and re_costumer='" + ddltype.Text + "' and re_status='" + ddlstatus.Text + "' and re_address='" + txtaddress.Text + "' and re_auther='" + txtauther.Text + "' and re_creation='" + txtdate.Text + "' and re_how='" + txthow.Text + "' and re_description='" + txtdescription.Text + "'", cn);
DataSet ds = new DataSet();
da.Fill(ds, "table");
Response.Redirect("Costumer.aspx");
خواهش میکنم کمکم کنید
ممنون از لطفتون

d_derakhshani
سه شنبه 19 اردیبهشت 1391, 08:55 صبح
کد شما سراسر ایراده.
اول ایرادی که گفتید. دوست گرامی datatable برای دستور select هست که داده هارو fetch میکنه. و به همین ترتیب دستور fill. در ضمن پارامتر متنی که در سازنده adapter میدید برای دستور Select هست. کلا در این مورد adapter زیاد کاربردی نداره. یک sqlcommand تعریف کنید و دستور delete رو بهش بدید. سپس متد executeNonQuery رو فراخوانی کنید.(از deleteCommand خود Adapter هم میشه استفاده کرد)
نکته بعدی اینکه شما باید یک کلید اصلی داشته باشید که حذف و ویرایش رو بر اساس اون انجام بدید نه اینکه به ازای تمامی فیلدها در Delete شرط where بزارید.
نکته آخر اینکه از پارامتر استفاده کنید. این طور کد زدن علاوه بر اینکه بسیار ناخواناست و توسعه و نگهداری برنامه رو سخت میکنه مشکل امنیتی هم داره

General-Xenon
سه شنبه 19 اردیبهشت 1391, 09:03 صبح
سلام
من یک گریدویو دارم در انتهای هر سطرگرید یک دکمه گذاشتم به نام انتخاب که مدیر باکلیک روی اون وارد صفحه دیگه ای میشه که میتونه تمام اطلاعات اون سطر انتخابی و به طور کامل ببینه
دراین قسمت تعدادی از فیلدها که فقط برای نمایش به مدیر است به صورت read only است وبعضی از فیلدها توسط مدیر قابل ویرایش است
درانتهای این صفحه یک دکمه بروزرسانی ویک دکمه حذف گذاشتم ، کدی که برای دکمه حذف نوشتم درست کارنمیکنه میشه لطف کنید ببینید عیبش چیه؟
این کدمه:
SqlDataAdapter da = new SqlDataAdapter("delete from t_register where re_fname='" + txtname.Text + "'and re_lname='" + txtlname.Text + "'and re_username='" + txtusername.Text + "'and re_pass='" + txtpass.Text + "' and re_companyname='" + txtcompanyname.Text + "' and re_type='" + txttype.Text + "' and re_typeofproperty='" + txtproperty.Text + "' and re_economiccode='" + txtcode.Text + "' and re_province='" + txtprovince.Text + "' and re_city='" + txtcity.Text + "' and re_website='" + txtweb.Text + "' and re_mobile='" + txtmobile.Text + "' and re_tell='" + txttell.Text + "' and re_fax='" + txtfax.Text + "' and re_email='" + txtemail.Text + "' and re_maritalstatus='" + txtmarital.Text + "' and re_sex='" + txtsex.Text + "' and re_birth='" + txtbirth.Text + "' and re_marriage='" + txtmarriage.Text + "' and re_degree='" + txtdegree.Text + "' and re_lists='" + txtlists.Text + "' and re_color='" + txtcolor.Text + "' and re_costumer='" + ddltype.Text + "' and re_status='" + ddlstatus.Text + "' and re_address='" + txtaddress.Text + "' and re_auther='" + txtauther.Text + "' and re_creation='" + txtdate.Text + "' and re_how='" + txthow.Text + "' and re_description='" + txtdescription.Text + "'", cn);
DataSet ds = new DataSet();
da.Fill(ds, "table");
Response.Redirect("Costumer.aspx");
خواهش میکنم کمکم کنید
ممنون از لطفتون

سلام..چرا این همه کد ؟؟؟
اگر این کد ها کار کنه، زمانی که کاربر یا همون مدیر سایتت یکی از این فیلد ها رو تغییر بده دیگه عملیاتت انجام نمیشه....

نمی دونم فیلد ID تو این Table داری یا نه ...اما میتونی با استفاده از یک ID این عملیات پاکسازی رو انجام بدی

اینجوری
SqlConnection _cn = new SqlConnection("Connection");
SqlCommand _cm = new SqlCommand("DELETE FROM [TABLE] WHERE ID=inja id recordeto bezar", _cn);
if (_cn.State != ConnectionState.Open) _cn.Open();
_cm.ExecuteNonQuery();
_cn.Close();

asiehmohammadian
سه شنبه 19 اردیبهشت 1391, 10:59 صبح
سلام..چرا این همه کد ؟؟؟
اگر این کد ها کار کنه، زمانی که کاربر یا همون مدیر سایتت یکی از این فیلد ها رو تغییر بده دیگه عملیاتت انجام نمیشه....

نمی دونم فیلد ID تو این Table داری یا نه ...اما میتونی با استفاده از یک ID این عملیات پاکسازی رو انجام بدی

اینجوری
SqlConnection _cn = new SqlConnection("Connection");
SqlCommand _cm = new SqlCommand("DELETE FROM [TABLE] WHERE ID=inja id recordeto bezar", _cn);
if (_cn.State != ConnectionState.Open) _cn.Open();
_cm.ExecuteNonQuery();
_cn.Close();

سلام
خیلی خیلی خیلی خیلی ازتون ممنونم واقعا نجاتم دادید مشکلم حل شد
بی نهایت سپاسگذارم:تشویق::لبخندساده: