简体   繁体   English

更新gridview后,用户输入在列中为空白

[英]User input is blank in column after updating gridview

After I click update in the grid view, the code works successfully. 在网格视图中单击更新后,代码成功运行。 But when I retrieve the data in another webpage the columns I updated are blank. 但是,当我在另一个网页中检索数据时,我更新的列为空白。 I have no clue why this happens. 我不知道为什么会这样。 Here is the code for both updating and displaying: this is for updating 这是用于更新和显示的代码:这是用于更新

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
         string v =     System.Configuration.ConfigurationManager.ConnectionStrings["harish"].ConnectionString;
         con = new OracleConnection(v);
         con.Open();

         string query = "update leave_module1 set name='"+GridView1.Rows[e.RowIndex].Cells[0].Text+"'";
         OracleCommand cmd = new OracleCommand(query, con);
         cmd.ExecuteNonQuery();
         ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('The Data has been added');</script>;");
         GridView1.EditIndex = -1;
         con.Close();    
}

and this the loadgrid() 这是loadgrid()

   protected void loadgrid()
{

     string v = System.Configuration.ConfigurationManager.ConnectionStrings["harish"].ConnectionString;
        con = new OracleConnection(v);
        con.Open(); 
        cmd = new OracleCommand("select *  from leave_module1 where '"+TextBox1.Text+"' between fd and td", con);
        dr = cmd.ExecuteReader();

        GridView1.DataSource=dr;
        GridView1.DataBind();
        con.Close();
        dr.Close();

}

You haven't mentioned any where clause in your update query , Query should be like this 您没有在update query提及任何where子句,查询应该像这样

Update tableName set ColumnName = 'Value'
where IDcolumn = 1

secondly you should work on Stored procedures instead of this approach. 其次,您应该使用存储过程而不是这种方法。

Blank column record must be because your code ( GridView1.Rows[e.RowIndex].Cells[0].Text ) is providing blank data, you should check it by debugging & watching it's value at runtime. 空列记录必须是因为您的代码( GridView1.Rows[e.RowIndex].Cells[0].Text )提供了空数据,您应该通过调试和在运行时观察其值来检查它。

your update statement has no where clause, so will update all of the rows in the table. 您的update语句没有where子句,因此将更新表中的所有行。 That might be what you want, might not. 那可能就是您想要的,也许不是。

your select statement may also be syntactically flawed. 您的select语句可能在语法上也有缺陷。 it would only successfully return rows where there is a column with the name of the value of TextBox1.Text that contains a value between the value in a column called fd and the value in a column called td . 它只会成功返回存在名为TextBox1.Text的值的列的行,该行的值介于fd列的值和td列的值之间。

These are the might be possible solutions or part of the whole solutions that I think would be helpful: 这些是可能的解决方案,或者是我认为会有所帮助的整个解决方案的一部分:

  • Check that loadgrid() isn't called before row_updating event because it will then cause the grid to lose changes and come back to normal view.. 检查row_updating事件之前未调用loadgrid(),因为这将导致网格丢失更改并返回正常视图。
  • Also check n understand from this link: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx 还要检查n从此链接是否理解: http : //msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowupdating.aspx
  • Don't keep sql query or connection string name coded in page behind, instead use stored procedure or use some class containing string constants accessible which are thru out solution. 不要将sql查询或连接字符串的名称保留在页面的后面,而应使用存储过程或使用某些包含可访问的字符串常量的类,这是解决方案。
  • Always use sql parameter and stop using string concats because it will cause sql infection issues. 始终使用sql参数,并停止使用字符串concats,因为这将导致sql感染问题。

When the page is post back the values of the gridviews flushed. 页面回发后,gridviews的值将刷新。 If you want to retain the values in Gridview you have to put these values in Viewstate for further operations. 如果要在Gridview中保留值,则必须将这些值放在Viewstate中以进行进一步的操作。 you have to use Viewstate.add() for adding your Gridview values in viewstate before postback. 在回发之前,必须使用Viewstate.add()在viewstate中添加Gridview值。 viewstate.add("key",value); viewstate.add(“ key”,value);

Hope it helps you. 希望对您有帮助。

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

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