无法将值更新到数据库

问题描述:

我有一个带有文本框的表单,当我在该文本框中编辑数据并将其更新到数据库时......编辑的数据没有更新到数据库。任何人都可以帮助我...下面是我的代码我尝试过:



i have a form with text boxes and when i edit data in that text box and update it to the database..the edited data are not getting updated to the database.can anyone help me please...below is my code which i tried:

 protected void Page_Load(object sender, EventArgs e)
    {
 
        if (File.Exists(Server.MapPath("~/advt/a.gif")) == true)
        {
            File.Delete(Server.MapPath("~/advt/a.gif"));

        }

        if (File.Exists(Server.MapPath("~/advt/b.gif")) == true)
        {
            File.Delete(Server.MapPath("~/advt/b.gif"));

        }
 try
        {

            My_Util Util = new My_Util();
            Util.SetConnection();
            Util.sql_con.Open();
 SqlCommand command = new SqlCommand("select Advimage,Name,CompanyName,EMail,PhoneNo,ContactBy,City,Area,Category,Convert(char(11),AdvStartdate,105)as AdvStartDate,Convert(char(11),AdvEnddate,105)as AdvEndDate,ImagePosition,ImageDimension from offsale.OS_ToAdvertise where Approval='False' and  datediff(day,AdvEnddate,getdate())<=0 and Name='" + Convert.ToString(Session["advertisename"]) + "' and CompanyName='" + Convert.ToString(Session["advertisecompname"]) + "' and City='" + Convert.ToString(Session["advertisecity"]) + "' and Area='" + Convert.ToString(Session["advertisearea"]) + "'", Util.sql_con);
 SqlDataAdapter adp = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
            {
                MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["Advimage"]);
                //MemoryStream ms=new MemoryStream (row ,Byte []);
                System.Drawing.Image i = System.Drawing.Image.FromStream(ms);

                i.Save(Server.MapPath("~/advt/advertiseview.gif"));

                Response.AppendHeader("Refresh", "1");


                if (File.Exists(Server.MapPath("~/advt/advertiseview.gif")) == false)
                {
                    Image1.ImageUrl = "~/Images/136.gif ";

                }
                else
                {
                    Image1.ImageUrl = "~/advt/advertiseview.gif";
                }
                txtContactPer.Text=Convert.ToString(dt.Rows[0]["Name"]);
                txtEdate.Text = Convert.ToString(dt.Rows[0]["AdvEndDate"]);
                txtSdate.Text = Convert.ToString(dt.Rows[0]["AdvStartDate"]);
                txtComname.Text = Convert.ToString(dt.Rows[0]["CompanyName"]);
                txtEmail.Text = Convert.ToString(dt.Rows[0]["EMail"]);
                txtPhone.Text = Convert.ToString(dt.Rows[0]["PhoneNo"]);
                conbytxtbox.Text = Convert.ToString(dt.Rows[0]["ContactBy"]);
                TextBoxposition.Text = Convert.ToString(dt.Rows[0]["ImagePosition"]);
                txtdimension.Text = Convert.ToString(dt.Rows[0]["Imagedimension"]);
            
            }
 }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }





及更新时点击下面的代码我试过:







and on update click the below code i tried:


try
        {
   My_Util util = new My_Util();
            util.SetConnection();
            util.sql_con.Open();
 SqlCommand command = new SqlCommand("update offsale.OS_ToAdvertise set Name ='" + txtContactPer.Text + "',CompanyName='" + txtComname.Text + "',PhoneNo='" + txtPhone.Text + "',ContactBy='" + conbytxtbox.Text + "',City='" + ddlone.Text + "',Area='" + ddltwo.Text + "',Category='" + rdoCate.Text + "',AdvStartDate='" + dstart.ToString("d") + "', AdvEndDate='" + dend.ToString("d") + "',ImagePosition='" + TextBoxposition.Text + "',Imagedimension='" + txtdimension.Text + "'where EMail='" + txtEmail.Text + "'", util.sql_con);
SqlDataAdapter adp = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            adp.Fill(dt);
        }
        catch (Exception ex)
        {

            Response.Write(ex.Message.ToString());
        }
 }

非常第一 - DataAdapter.Fill方法不能与 UPDATE 命令。

Fill 方法使用由$指定的 SELECT 语句从数据源中检索行。关联的 SelectCommand 属性。与 SELECT 语句关联的连接对象必须有效,但不需要打开它。如果在调用 Fill 之前关闭连接,则会打开它以检索数据,然后关闭。如果在调用Fill之前连接已打开,它将保持打开状态。



您应该使用 ExecuteNonQuery UPDATE 数据库。



Very first- DataAdapter.Fill method cannot be used with UPDATE Command.
The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.

You should use ExecuteNonQuery to UPDATE database.

My_Util util = new My_Util();
util.SetConnection();
util.sql_con.Open(); 
SqlCommand command = new SqlCommand("update offsale.OS_ToAdvertise set Name ='" + txtContactPer.Text + "',CompanyName='" + txtComname.Text + "',PhoneNo='" + txtPhone.Text + "',ContactBy='" + conbytxtbox.Text + "',City='" + ddlone.Text + "',Area='" + ddltwo.Text + "',Category='" + rdoCate.Text + "',AdvStartDate='" + dstart.ToString("d") + "', AdvEndDate='" + dend.ToString("d") + "',ImagePosition='" + TextBoxposition.Text + "',Imagedimension='" + txtdimension.Text + "'where EMail='" + txtEmail.Text + "'", util.sql_con);
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}