在gridview中保存数据后,在gridview中进行更改 [英] After saving data in gridview making changes in gridview

查看:76
本文介绍了在gridview中保存数据后,在gridview中进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class CreateChallan : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["amitpandeyConnectionString2"].ConnectionString;
      protected void btnUpdate_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);

            SqlConnection con = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)
                   GridView1.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkUpdate != null)
                {
                    if (chkUpdate.Checked)
                    {

                        string strDocketNo = GridView1.Rows[i].Cells[1].Text;

                        string strBranchCode = ((TextBox)GridView1.Rows[i].FindControl("txtBranchCode")).Text;

                        string strDate = ((TextBox)GridView1.Rows[i].FindControl("txtDate")).Text;

                        string strPKTS = ((TextBox)GridView1.Rows[i].FindControl("txtPKTS")).Text;

                        string strTo = ((TextBox)GridView1.Rows[i].FindControl("txtTo")).Text;

                        string strActWt = ((TextBox)GridView1.Rows[i].FindControl("txtActwt")).Text;

                        string strChargeWt = ((TextBox)GridView1.Rows[i].FindControl("txtChargeWt")).Text;

                        string strMode = ((TextBox)GridView1.Rows[i].FindControl("Mode")).Text;

                        string strChallanNo = ((TextBox)GridView1.Rows[i].FindControl("ChallanNo")).Text;

                        string strVehicleNo = ((TextBox)GridView1.Rows[i].FindControl("VehicleNo")).Text;

                        string strChallanDate = ((TextBox)GridView1.Rows[i].FindControl("ChallanDate")).Text;

                        string strDescription = ((TextBox)GridView1.Rows[i].FindControl("Description")).Text;                      

                        string strUpdate ="Update CreateDocket set BranchCode = '" + strBranchCode + "', Date = '" + strDate + "', PKTS = '" + strPKTS + "',To = '"+strTo+"',ActWt = '" + strActWt + "',ChargeWt = '" + strChargeWt + "',Mode = '" + strMode + "',ChallanNo = '" + strChallanNo + "',VehicleNo = '" + strVehicleNo + "',ChallanDate = '" + strChallanDate + "',Description = '" + strDescription + "' WHERE DocketNo ='" + strDocketNo + "'";
                        strSql.Append(strUpdate);
                    }
                }
            }
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql.ToString();
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string errorMsg = "Error in Updation";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                con.Close();
            }
            
        }
       
        protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
        {
            CheckBox chkTest = (CheckBox)sender;
            GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;

            TextBox txtBranchCode = (TextBox)grdRow.FindControl("txtBranchCode");

            TextBox txtDate = (TextBox)grdRow.FindControl("txtDate");

            TextBox txtPKTS = (TextBox)grdRow.FindControl("txtPKTS");

            TextBox txtTo = (TextBox)grdRow.FindControl("txtTo");

            TextBox txtActWT = (TextBox)grdRow.FindControl("txtActWT");

            TextBox txtChargeWt = (TextBox)grdRow.FindControl("txtChargeWt");

            TextBox txtMode = (TextBox)grdRow.FindControl("txtMode");

            TextBox txtChallanNo = (TextBox)grdRow.FindControl("txtChallanNo");

            TextBox txtVehicleNo = (TextBox)grdRow.FindControl("txtVehicleNo");

            TextBox txtChallanDate = (TextBox)grdRow.FindControl("txtChallanDate");            

            TextBox txtDescription = (TextBox)grdRow.FindControl("txtDescription");

            if (chkTest.Checked)
            {
              
                txtChallanNo.ReadOnly = false;
                txtVehicleNo.ReadOnly = false;
                txtChallanDate.ReadOnly = false;                
                txtDescription.ReadOnly = false;
                txtChallanNo.ForeColor = System.Drawing.Color.Red;
                txtVehicleNo.ForeColor = System.Drawing.Color.Red;
                txtChallanDate.ForeColor = System.Drawing.Color.Red;                
                txtDescription.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                txtChallanNo.ReadOnly = true;
                txtVehicleNo.ReadOnly = true;
                txtChallanDate.ReadOnly = true;                
                txtDescription.ReadOnly = true;
                txtChallanNo.ForeColor = System.Drawing.Color.Green;
                txtVehicleNo.ForeColor = System.Drawing.Color.Green;
                txtChallanDate.ForeColor = System.Drawing.Color.Green;                
                txtDescription.ForeColor = System.Drawing.Color.Green;  
                
            }
        }
    }





我做的是我做的最后一页中的一些条目,我已经为下一页留下了一些条目但我的问题是现在,当我在文本框中写入时,它向我显示两个错误第一个是对象引用未设置为对象的实例。对于VehicleNO

第二是它显示空参考对于ChallanNO

请帮助我。



What I have Done is I Have made some entries in last page and I have left some entries for next page But my problem is Now when I write in text boxes then its showing me two error 1st is "Object reference not set to an instance of an object". for VehicleNO
2nd is It is showing me null reference For ChallanNO
Please Help me .

推荐答案

你在一个地方使用 txt ChallanNo,在另一个地方使用ChallanNo - 无论你的控件实际被调用,你需要编辑另一个匹配的字符串。
You use "txtChallanNo" in one place and "ChallanNo" in another - whichever your control is actually called, you need to edit the other string to match.


添加到 Duncan Edwards Jones 说的,我只是指出了问题。



btnUpdate_Click 活动,你有......

Adding to what Duncan Edwards Jones said, I am just pointing out the problem.

Inside the btnUpdate_Click Event, you have ...
string strMode = ((TextBox)GridView1.Rows[i].FindControl("Mode")).Text;

string strChallanNo = ((TextBox)GridView1.Rows[i].FindControl("ChallanNo")).Text;

string strVehicleNo = ((TextBox)GridView1.Rows[i].FindControl("VehicleNo")).Text;

string strChallanDate = ((TextBox)GridView1.Rows[i].FindControl("ChallanDate")).Text;

string strDescription = ((TextBox)GridView1.Rows[i].FindControl("Description")).Text;



但是在 chkSelect_CheckedChanged 事件,你有...


But in chkSelect_CheckedChanged Event, you have...

TextBox txtMode = (TextBox)grdRow.FindControl("txtMode");
 
TextBox txtChallanNo = (TextBox)grdRow.FindControl("txtChallanNo");

TextBox txtVehicleNo = (TextBox)grdRow.FindControl("txtVehicleNo");

TextBox txtChallanDate = (TextBox)grdRow.FindControl("txtChallanDate");            

TextBox txtDescription = (TextBox)grdRow.FindControl("txtDescription");





请参阅控制ID不同。其中一个是正确的。



See that the Control IDs are different. One of them is correct.


这篇关于在gridview中保存数据后,在gridview中进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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