PDA

View Full Version : سوال: حذف نشدن اطلاعات از دیتابیس و گریدویو



Saman Ice
یک شنبه 07 خرداد 1391, 16:34 عصر
سلام
من این کد رو نوشتم برای حذف رکوردهای که تو گرید انتخاب شدن، ولی حذف نمیکنه و هیچ خطایی هم نمیده

string strID = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = (GridView1.Rows[i].Cells[5].Text).ToString();
SqlConnection conn = new SqlConnection("Data Source=saman-pc;Initial Catalog=University;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string qeury = "DELETE FROM selectunits WHERE studentid='" + int.Parse(idnum.Text) + "' and lessonid='" + int.Parse(strID) + "'";
cmd.CommandText = qeury;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
GridView1.DataBind();
}

mas'oud
یک شنبه 07 خرداد 1391, 17:15 عصر
خیلی راهنماییتون کمه!
تنها حدسی که تونستم بزنم اینه که شاید int.Parse(idnum.Text) و int.Parse(strID) رو جابجا نوشتید!

Saman Ice
یک شنبه 07 خرداد 1391, 17:18 عصر
یه گرید ویو دارم. بهش چک باکس اضافه کردم. حالا میخوام کاربر تیک هرکدوم رو که زد فقط اونا حذف بشن
با این کد insert رو نوشتم و خیلی خوب کار میکنه. ولی برای جذف کار نمیکنه

fakhravari
یک شنبه 07 خرداد 1391, 22:55 عصر
private void BindGrid()
{
using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConStr"].ConnectionString))
{

using (SqlCommand Cmd = new SqlCommand("FetchData", Con))
{
Cmd.CommandType = CommandType.StoredProcedure;
Con.Open();
SqlDataReader Dr = Cmd.ExecuteReader();

GridView1.DataSource = Dr;
GridView1.DataBind();
Dr.Close();
Con.Close();
}
}
}
private void RemoveData(string RowID)
{

using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConStr"].ConnectionString))
{

using (SqlCommand Cmd = new SqlCommand("RemoveData", Con))
{
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@MsgID", RowID);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

BindGrid();
}
}
protected void Delete_Click(object sender, EventArgs e)
{
string RowID = "";

foreach (GridViewRow myrow in GridView1.Rows)
{

CheckBox ChkSelect = (CheckBox)myrow.Cells[0].FindControl("ChkSelect");
if (ChkSelect.Checked)
RowID += GridView1.DataKeys[myrow.RowIndex].Value.ToString() + ",";
}

if (RowID.Length != 0)
RowID = RowID.Remove(RowID.Length - 1);

RemoveData(RowID);
BindGrid();
}

Mcraft
سه شنبه 09 خرداد 1391, 16:48 عصر
سلام .
میشه لطفا vb رو هم بزارید ؟؟؟
البته به این شکل که هر تعداد چک باکس فعال بود ، پاک بشن.
یک دنیا ممنون.

fakhravari
سه شنبه 09 خرداد 1391, 16:57 عصر
سایت تبدیل http://www.developerfusion.com/tools/convert/vb-to-csharp