更新网格视图数据 [英] Updating Grid view Data

查看:66
本文介绍了更新网格视图数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,用于直接从网格视图数据更新和保存.但是它不起作用.
帮我更新记录.提前感谢.在我犯错误的地方,我无法找出错误.

This is my code for updating and saving from grid view data directly.But it is not working.
Help me out to update record.thanks in advance.Where i am making mistake im not able to find out.

protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
   {
       string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[0].Text;
       string name=((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string address = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string dept = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       string mail = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
       updaterecordMethod(name,address,dept,mail);
       gridRegistrationTableDetails.EditIndex = -1;
       BindData();
   }

   private void updaterecordMethod(string name,string address,string dept,string mail)
   {
       string UpdateQuery = "update SignUP set Employee_Name=@Empname,Employee_Address=@EmpAddress,Employee_Dept=@EmpDept,Employee_Mail=@EmpMail";
       try
       {
           SqlConObject.Open();
           SqlCommand cmd = new SqlCommand(UpdateQuery, SqlConObject);
           cmd.Parameters.AddWithValue("@Empname", name);
           cmd.Parameters.AddWithValue("@EmpAddress", address);
           cmd.Parameters.AddWithValue("@EmpDept", dept);
           cmd.Parameters.AddWithValue("@EmpMail", mail);
           cmd.CommandType = CommandType.Text;
           cmd.ExecuteNonQuery();
       }
       finally
       {
           SqlConObject.Close();
       }

   }

推荐答案

请检查Gridview值的转换.您正在为字符串变量分配值.我确定您在构建页面时会出错.

Please check out the Casting of the Gridview values. you are assigning values to string variable. I am sure you are getting error while building a page.

protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[0].Text;
        Textbox name=((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        Textbox address = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        Textbox dept = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        Textbox mail = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        updaterecordMethod(name.Text.Trim(),address.Text.Trim(),dept.Text.Trim(),mail.Text.Trim());
        gridRegistrationTableDetails.EditIndex = -1;
        BindData();
    }




试试这个


谢谢
Ashish




Try this


Thanks
Ashish


此代码无法正常工作Ashish .. !!
this code is not working Ashish..!


我明白了.这是代码....

i got it. this is the code....

protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[2].Text;
        string name = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string address = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        string dept = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        string mail = ((TextBox)gridRegistrationTableDetails.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        updaterecordMethod(name, address, dept, mail);
        gridRegistrationTableDetails.EditIndex = -1;
        BindData();
    }
    private void updaterecordMethod(string name,string address,string dept,string mail)
    {
        string UpdateQuery = "update ISSSignUP set Employee_Name=@Empname,Employee_Address=@EmpAddress,Employee_Email=@EmpMail,Employee_Department=@EmpDept";
        try
        {
            SqlConObject.Open();
            SqlCommand cmd = new SqlCommand(UpdateQuery, SqlConObject);
            cmd.Parameters.AddWithValue("@Empname", name);
            cmd.Parameters.AddWithValue("@EmpAddress", address);
            cmd.Parameters.AddWithValue("@EmpMail", mail);
            cmd.Parameters.AddWithValue("@EmpDept", dept);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        finally
        {
            SqlConObject.Close();
        }

    }


这篇关于更新网格视图数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆