behnam-p
سه شنبه 27 تیر 1391, 20: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();
}
الان مشکلم اینه وقتی روی دکمه ویرایش و حذف کلیک می کنم هیچ اتفاقی نمی افته و مثلا نه اطلاعات از خود صفحه و نه در بانکش حذف نمی شن!
به نظرتون مشکل از کجاست؟ و باید چی کار کنم؟
من در برنامه هام در یه صفحه اطلاعات رو دریافت می کنم که کدش به این شکله:
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();
}
الان مشکلم اینه وقتی روی دکمه ویرایش و حذف کلیک می کنم هیچ اتفاقی نمی افته و مثلا نه اطلاعات از خود صفحه و نه در بانکش حذف نمی شن!
به نظرتون مشکل از کجاست؟ و باید چی کار کنم؟