PDA

View Full Version : سوال: عمل نکردن حذف و ویرایش در Grdidview



behnam-p
سه شنبه 27 تیر 1391, 19:27 عصر
سلام
من در برنامه هام در یه صفحه اطلاعات رو دریافت می کنم که کدش به این شکله:

string filename = "nopic.gif";
if (FileUpload1.HasFile)
{
filename = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("pics\\") + filename);
}
SqlConnection con = new SqlConnection("data source=PROBOOK-PC;initial catalog=ll;integrated security=true");

string query = "insert into [table](title,abs,contents,pic) values(@title,@abs,@contents,@pic)";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@title", TextBox1.Text);
cmd.Parameters.AddWithValue("@abs", TextBox2.Text);
cmd.Parameters.AddWithValue("@contents",TextBox3.Text);
cmd.Parameters.AddWithValue("@pic", filename);
con.Open();

cmd.ExecuteNonQuery();

con.Close();

TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";


و در این صفحه هم اطلاعات در گرید ویو نمایش داده می شه که کدش به این شکله:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
public void BindGridView()
{

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");

string query = "select * from [table]";

SqlCommand cmd = new SqlCommand(query, con);

con.Open();

SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();

con.Close();
}

public void RemovePic(int id)
{
SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");
string query = "select pic from [table] where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", id);

con.Open();
string fileName = cmd.ExecuteScalar().ToString();
con.Close();

if (fileName != "nopic.gif")
File.Delete(Server.MapPath("Pics\\") + fileName);

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "remove")
{
int id = Convert.ToInt32(e.CommandArgument);
RemovePic(id);

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");
string query = "delete from [table] where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", id);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

BindGridView();
}
else if (e.CommandName == "change")
{
int id = Convert.ToInt32(e.CommandArgument);

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");
string query = "select * from [table] where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", id);

con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();

TextBox1.Text = dr["title"].ToString();
TextBox2.Text = dr["abs"].ToString();
TextBox3.Text = dr["contents"].ToString();
FileUpload1.ToolTip = dr["pic"].ToString();


dr.Close();
con.Close();

ViewState["id"] = id;

MultiView1.ActiveViewIndex = 1;

}
}


protected void Button1_Click1(object sender, EventArgs e)
{
int id = Convert.ToInt32(ViewState["id"]);

string filename = FileUpload1.ToolTip;

if (FileUpload1.HasFile)
{
filename = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("pics\\") + filename);
}

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");

string query = "update table set title=@title,abstract=@abs,contents=@contents,pic= @pic where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@title", TextBox1.Text);
cmd.Parameters.AddWithValue("@abs", TextBox2.Text);
cmd.Parameters.AddWithValue("@contents", TextBox3.Text);
cmd.Parameters.AddWithValue("@Pic", filename);
cmd.Parameters.AddWithValue("@id", id);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}




الان مشکلم اینه وقتی روی دکمه ویرایش و حذف کلیک می کنم هیچ اتفاقی نمی افته و مثلا نه اطلاعات از خود صفحه و نه در بانکش حذف نمی شن!
به نظرتون مشکل از کجاست؟ و باید چی کار کنم؟

konkoory_82
چهارشنبه 28 تیر 1391, 09:10 صبح
سلام
من در برنامه هام در یه صفحه اطلاعات رو دریافت می کنم که کدش به این شکله:

string filename = "nopic.gif";
if (FileUpload1.HasFile)
{
filename = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("pics\\") + filename);
}
SqlConnection con = new SqlConnection("data source=PROBOOK-PC;initial catalog=ll;integrated security=true");

string query = "insert into [table](title,abs,contents,pic) values(@title,@abs,@contents,@pic)";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@title", TextBox1.Text);
cmd.Parameters.AddWithValue("@abs", TextBox2.Text);
cmd.Parameters.AddWithValue("@contents",TextBox3.Text);
cmd.Parameters.AddWithValue("@pic", filename);
con.Open();

cmd.ExecuteNonQuery();

con.Close();

TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";


و در این صفحه هم اطلاعات در گرید ویو نمایش داده می شه که کدش به این شکله:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
public void BindGridView()
{

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");

string query = "select * from [table]";

SqlCommand cmd = new SqlCommand(query, con);

con.Open();

SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();

con.Close();
}

public void RemovePic(int id)
{
SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");
string query = "select pic from [table] where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", id);

con.Open();
string fileName = cmd.ExecuteScalar().ToString();
con.Close();

if (fileName != "nopic.gif")
File.Delete(Server.MapPath("Pics\\") + fileName);

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "remove")
{
int id = Convert.ToInt32(e.CommandArgument);
RemovePic(id);

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");
string query = "delete from [table] where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", id);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

BindGridView();
}
else if (e.CommandName == "change")
{
int id = Convert.ToInt32(e.CommandArgument);

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");
string query = "select * from [table] where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", id);

con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();

TextBox1.Text = dr["title"].ToString();
TextBox2.Text = dr["abs"].ToString();
TextBox3.Text = dr["contents"].ToString();
FileUpload1.ToolTip = dr["pic"].ToString();


dr.Close();
con.Close();

ViewState["id"] = id;

MultiView1.ActiveViewIndex = 1;

}
}


protected void Button1_Click1(object sender, EventArgs e)
{
int id = Convert.ToInt32(ViewState["id"]);

string filename = FileUpload1.ToolTip;

if (FileUpload1.HasFile)
{
filename = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("pics\\") + filename);
}

SqlConnection con = new SqlConnection("data source=probook-pc;initial catalog=ll;integrated security=true");

string query = "update table set title=@title,abstract=@abs,contents=@contents,pic= @pic where id=@id";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@title", TextBox1.Text);
cmd.Parameters.AddWithValue("@abs", TextBox2.Text);
cmd.Parameters.AddWithValue("@contents", TextBox3.Text);
cmd.Parameters.AddWithValue("@Pic", filename);
cmd.Parameters.AddWithValue("@id", id);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}




الان مشکلم اینه وقتی روی دکمه ویرایش و حذف کلیک می کنم هیچ اتفاقی نمی افته و مثلا نه اطلاعات از خود صفحه و نه در بانکش حذف نمی شن!
به نظرتون مشکل از کجاست؟ و باید چی کار کنم؟

بهتره برنامه ات رو trace کنی و بخش هایی که با دیتابیس کار می کنه مثه باز و بسته کردن connecion و اجرای کامند رو درون try , catch بذاری و پغام خطایی رو که catch بهت می ده رو بررسی کنی .

karim orooji
چهارشنبه 28 تیر 1391, 09:29 صبح
سلام
برنامه رو ضمیمه کن
تا دوستان بهتر بتونند روش نظر بدند

behnam-p
چهارشنبه 28 تیر 1391, 11:06 صبح
اینم فایل ضمیمه:
89922

bftarane
چهارشنبه 28 تیر 1391, 13:03 عصر
سلام.

onrowcommand="GridView1_RowCommand"باید اضافه بشه به صورت زیر:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%"
onrowcommand="GridView1_RowCommand">
.......
با دابل کلیک هم می تونستی اضافه کنی حتماً بلدی دیگه.