简体   繁体   English

数据表不会更新

[英]datatable won't update

I wanted to update my database that contains two text and one filename that is needed for image. 我想更新数据库,其中包含两个文本和一个图像所需的文件名。

The problem is that the image and filename updates but the two other text values title and body wont be affected and don't change the previous values. 问题在于图像和文件名会更新,但其他两个文本值的标题和正文将不会受到影响,并且不会更改以前的值。 Also visual studio don't get any problem and the message for executing command shows that it's executed the command but nothing except the image changes. Visual Studio也没有任何问题,执行命令的消息表明它已经执行了命令,但是除了图像改变外什么都没有。

    protected void Page_Load(object sender, EventArgs e)
    {


        if (Session["user"] == null)
            Response.Redirect("~/default.aspx");

        if (Request .QueryString ["action"]=="edit")
        {
            Panel1.Visible = true;
        }

        if (Request.QueryString["edit"] != null)
        {


            Panel1.Visible = true;
            SqlConnection con2 = new SqlConnection();
            con2.ConnectionString =GNews.Properties.Settings.Default.connectionstring;
            DataTable dt3 = new DataTable();
            con2.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select * from loadpost_view where Postid=" + Request.QueryString["edit"].ToString () + "", con2);
            myReader = myCommand.ExecuteReader();



            while (myReader.Read())
            {
                title_txt.Text=myReader ["Title"].ToString ();
                bodytxt.Text = myReader["Body"].ToString();
            }
            con2.Close();
        }
protected void btn_addpost_Click(object sender, EventArgs e)
    {
string title= title_txt .Text ;
string body=bodytxt .Text ;

if (Request.QueryString["edit"] != null)
{
    string message;

    string filename = thumb_uploader.FileName;
    string path = HttpContext.Current.Server.MapPath("~") + "\\Thumb";
    string exup = System.IO.Path.GetExtension(thumb_uploader.FileName);
    string[] ext = { ".jpg", ".png", ".jpeg" };
    if (Array.IndexOf(ext, exup) < 0)
    {
        message = "not correct.";
    }

    if (thumb_uploader.FileBytes.Length / 1024 > 400)
    {
        message = "not currect.";
    }

    while (System.IO.File.Exists(path + "\\" + filename + exup))
    {
        filename += "1";
    }

    savepath = path + "\\" + filename;

    if (thumb_uploader.HasFile)
    {
        thumb_uploader.SaveAs(savepath);

        thumb = thumb_uploader.FileName;
        SqlCommand command;
        SqlDataAdapter da;
        SqlConnection con3 = new SqlConnection();

        con3.ConnectionString = GNews.Properties.Settings.Default.connectionstring;
        command = new SqlCommand();
        command.Connection = con3;
        da = new SqlDataAdapter();
        da.SelectCommand = command;

        command.CommandText = "UPDATE tbl_post SET Title=@title ,Body=@body ,Thumb=@thu Where Postid=" + Request.QueryString["edit"].ToString();
        con3.Open();
        command.Parameters.AddWithValue("@title", title );
        command.Parameters.AddWithValue("@body", body );
        command.Parameters.AddWithValue("@thu", thumb_uploader .FileName);

        command.ExecuteNonQuery();
        con3.Close();
        message = "its ok.";
        lbl_result.Text = message;
   }
   else
   {
        using (SqlConnection con3 = new SqlConnection(GNews.Properties.Settings.Default.connectionstring))
        {
            string sql = "update tbl_post SET Title=@title ,Body=@body  Where Postid=@postid" ;
            using (SqlCommand command = new SqlCommand(sql, con3))
            {
                con3.Open();

                command.Parameters.AddWithValue("@title", title);
                command.Parameters.AddWithValue("@body", body);
                command.Parameters.AddWithValue("@postid",  Request.QueryString["edit"].ToString());

                command.ExecuteNonQuery();
                con3.Close();
                message = "its ok.";
                lbl_result.Text = message;
            }
        }
   }
}

I've found the answer I needed this code to include my pageload reading database so it wouldn't do it when I click on the update button.I mean the problem was all about the post back thing. 我找到了答案,我需要这段代码来包含我的页面加载阅读数据库,因此当我单击更新按钮时它不会这样做。我的意思是问题全在回发上。

 if (!Page.IsPostBack)
        {
            if (Request.QueryString["edit"] != null)
            {


                Panel1.Visible = true;
                SqlConnection con2 = new SqlConnection();
                con2.ConnectionString = GNews.Properties.Settings.Default.connectionstring;
                DataTable dt3 = new DataTable();
                con2.Open();
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("select * from loadpost_view where Postid=" + Request.QueryString["edit"].ToString() + "", con2);
                myReader = myCommand.ExecuteReader();



                while (myReader.Read())
                {
                    title_txt.Text = myReader["Title"].ToString();
                    bodytxt.Text = myReader["Body"].ToString();
                }
                con2.Close();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM