Click here to Skip to main content
15,171,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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;  
                
            }
        }
    }


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 .
Posted
Updated 2-Dec-14 3:59am
v2
Comments
Richard Deeming 2-Dec-14 9:23am
   
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
   
First of all, use Parameterized query to avoid the SQL Injection Attack.

While line throws you exception?

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.
   
Comments
   
+5. I have pointed out the problem in my answer. :)
Adding to what Duncan Edwards Jones said, I am just pointing out the problem.

Inside the btnUpdate_Click Event, you have ...
C#
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;

But in chkSelect_CheckedChanged Event, you have...
C#
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");


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900