Click here to Skip to main content
15,890,557 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 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 strChallanDate = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanDate")).Text;

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

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

                       


                        string strUpdate =
                            "Update CreateDocket set BranchCode = '" + strBranchCode + "', Date = '" + strDate + "', PKTS = '" + strPKTS + "',ActWt = '" + strActWt + "',ChargeWt = '" + strChargeWt + "',Mode = '" + strMode + "',ChallanNo = '" + strChallanNo + "',ChallanDate = '" + strChallanDate + "',VehicleNo = '" + strVehicleNo + "',Description = '" + strDescription + "' WHERE DEocketNo ='" + 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 txtActWT = (TextBox)grdRow.FindControl
                                              ("txtActWT");

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

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

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

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

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

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

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

Intially I was reciving error as object not set to the instance it was in the line txtVehicleNo.ReadOnly = false; but when I removed that line then It showed me the fillowing error.
Posted
Updated 1-Dec-14 19:52pm
v2
Comments
[no name] 2-Dec-14 2:05am    
print your strUpdate, and check..
Member 11111143 2-Dec-14 2:19am    
Please tell me a bit more. I din't got you totally. Thanks for your concern
[no name] 2-Dec-14 2:33am    
Before ExecuteNonQuery() line check where the commandtext is properly assigned and u can check it by printing it on page/file or view it in debug mode. once u are sure that update command is properly assigned we can refactor your problem quicky.
Marcin Kozub 2-Dec-14 2:57am    
A little bit off topic:
1. You should always use parametrized queries for safety and code readability
2. Why are you using StringBuilder here?

1 solution

there may be a situation where you may not have value for strSql.ToString() then you may end up with empty sql statement for SQL command. debug and check why your code not behave as expected.
 
Share this answer
 

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