Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateChallan.aspx.cs" Inherits="CreateChallan" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

</head >
<body onload="GetDateTime()">
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server"
                AllowPaging="True"
                DataSourceID="SqlDataSource1"
                AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333">
                <AlternatingRowStyle BackColor="White" />
                <Columns>

                    <asp:TemplateField HeaderText="Select">


                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server"
                                AutoPostBack="true"
                                OnCheckedChanged="chkSelect_CheckedChanged" />
                        </ItemTemplate>

                    </asp:TemplateField>
                    <asp:BoundField DataField="DocketNo" HeaderText="DocketNo"
                        SortExpression="ID" />

                    <asp:TemplateField HeaderText="BranchCode"
                        SortExpression="BranchCode">
                        <ItemTemplate>
                            <asp:TextBox ID="txtBranchCode" runat="server"
                                Text='<%# Bind("BranchCode") %>' ReadOnly="true"
                                ForeColor="Blue" BorderStyle="none"
                                BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="Date"
                        SortExpression="Date">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDate" runat="server"
                                Text='<%# Bind("Date") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                        <asp:TemplateField HeaderText="PKTS"
                        SortExpression="PKTS">
                        <ItemTemplate>
                            <asp:TextBox ID="txtPKTS" runat="server"
                                Text='<%# Bind("PKTS") %>' ReadOnly="true"
                                ForeColor="Blue" BorderStyle="none"
                                BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="To"
                        SortExpression="To">
                        <ItemTemplate>
                            <asp:TextBox ID="txtTo" runat="server"
                                Text='<%# Bind("To") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>



                    <asp:TemplateField HeaderText="ActWT"
                        SortExpression="ActWt">
                        <ItemTemplate>
                            <asp:TextBox ID="txtActWt" runat="server"
                                Text='<%# Bind("ActWt") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="ChargeWt"
                        SortExpression="ChargeWt">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChargeWt" runat="server"
                                Text='<%# Bind("ChargeWt") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Mode"
                        SortExpression="Mode">
                        <ItemTemplate>
                            <asp:TextBox ID="Mode" runat="server"
                                Text='<%# Bind("Mode") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="ChallanNo"
                        SortExpression="ChallanNo">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChallanNo" runat="server"
                                Text='<%# Bind("ChallanNo") %>'
                                ReadOnly="false" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="VehicleNo" SortExpression="VehicleNo">
                        <ItemTemplate>
                            <asp:TextBox ID="VehicleNo" runat="server"
                                Text='<%# Bind("VehicleNo") %>'
                                ReadOnly="False" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="ChallanDate"
                        SortExpression="ChallanDate">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChallanDate" runat="server"
                                Text='<%# String.Format("{0} - {1}", Eval("ChallanDate"), System.DateTime.Now.ToString()) %>'

                                ReadOnly="False" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

                     <asp:TemplateField HeaderText="Description"
                        SortExpression="Description">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDescription" runat="server"
                                Text='<%# Bind("Description") %>'
                                ReadOnly="False" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                         </asp:TemplateField>




                </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>
            SELECT FROM
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:amitpandeyConnectionString2 %>"
                SelectCommand="SELECT [Date], [DocketNo], [To], [PKTS], [ActWt], [ChargeWt], [Mode], [BranchCode],[ChallanNo] ,[ChallanDate],[VehicleNo], [Description]   FROM [CreateDocket]"
                DeleteCommand="DELETE FROM product WHERE (DocketNo= @DocketNo)"
                UpdateCommand="UPDATE [CreateDocket] SET [To] = @To,
              [Date] = @Date,[PKTS]=@PKTS,[BranchCode] = @BranchCode,[ActWt] = @ActWt,[ChargeWt]=@ChargeWt,[Mode]=@Mode,[ChallanNo]=@ChallanNo,[ChallanDate]=@ChallanDate,[VehicleNo]=@VehicleNo,[Description]=@Description WHERE [DocketNo] = @DocketNo">
                <DeleteParameters>
                    <asp:Parameter Name="DocketNo" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Date" />
                    <asp:Parameter Name="PKTS" />
                    <asp:Parameter Name="To" />
                    <asp:Parameter Name="BranchCode" />
                    <asp:Parameter Name="ActWt" />
                    <asp:Parameter Name="ChargeWT" />
                    <asp:Parameter Name="Mode" />
                     <asp:Parameter Name="ChallanNo" />
                    <asp:Parameter Name="ChallanDate" />
                    <asp:Parameter Name="VehicleNo" />
                    <asp:Parameter Name="Description" />

                </UpdateParameters>
            </asp:SqlDataSource>
            <br />

            <asp:Button ID="btnUpdate" runat="server"
                OnClick="btnUpdate_Click" Text="Save" /><br />


        </div>
    </form>
</body>
</html>


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("txtMode")).Text;

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

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

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

string strDescription = ((TextBox)GridView1.Rows[i].FindControl("txtDescription")).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;

}
}
}
the error I am getting is:
CSS
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:
Line 117:                txtVehicleNo.ReadOnly = false;
I am not really able to make out what is the problem in this page.When I try to remove the If case then it is showing me error on line cmd.ExecuteNonQuery(); with error: "ExecuteNonQuery: CommandText property has not been initialized".
Posted

1.This kind of error occurs when you are trying to access an object that does not exist.

2.In you case you are trying to use an null object named txtVehicleNo. You are trying to find this text box by its ID "txtVehicleNo", but this control is not defined in the ASP page. So you have to define this TextBox in the web page by using the same ID then this error will be fixed!
 
Share this answer
 
If the exception is really thrown in the line you show, it means that txtVehicleNo is null. You try to dereference it as it was an object, by assigning a value to its instance property, but there is no an instance.

You don't have to ask a question every time you face null reference exception. Instead, it's really important to learn how to deal with such situations all by yourself. Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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