Sunil Kumar

Sunil Kumar

  • NA
  • 22
  • 584

Query string in a textbox and submit to data source

Nov 25 2019 11:17 PM
I have a redirect page url "http://127.0.0.1/HOG/HOG_reporting_Rly.aspx?Monday=Y&Zrly=CR&dt=01-Apr-19" for rendering another aspx page having grid view (based on query parameters). Now I am trying to set query string "dt" date to textbox in edit mode so that data could be updated but it giving error 
 

Object reference not set to an instance of an object.

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.  
 
Here is my entire code: 
 
  1. <%@ Page Language="C#" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <script runat="server">  
  6.     public void Page_Load(object sender, EventArgs e)  
  7.     {  
  8.   
  9.         if (!Page.IsPostBack)  
  10.         {  
  11.             if (Request.QueryString["dt"] != null)  
  12.             {  
  13.                 ((TextBox)GridView1.FindControl("TextBox17")).Text = Request.QueryString["dt"];  
  14.             }  
  15.   
  16.         }  
  17.     }  
  18. </script>  
  19.   
  20. <html xmlns="http://www.w3.org/1999/xhtml">  
  21. <head runat="server">  
  22.     <title></title>  
  23.     <style type="text/css">  
  24.         .auto-style1 {  
  25.             text-align: center;  
  26.         }  
  27.   
  28.         .auto-style2 {  
  29.             font-family: Tahoma;  
  30.             font-size: medium;  
  31.             color: #990000;  
  32.             font-weight: bold;  
  33.         }  
  34.     </style>  
  35. </head>  
  36. <body>  
  37.     <form id="form1" runat="server">  
  38.         <div class="auto-style1">  
  39.   
  40.             <span class="auto-style2">HOG Train Master (List of All HOG Trains)</span><br class="auto-style2" />  
  41.         </div>  
  42.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"   
  43.              HorizontalAlign="Center" Font-Names="Tahoma" Font-Size="Small" AllowSorting="True" >  
  44.             <Columns>  
  45.                 <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />  
  46.                 <asp:TemplateField HeaderText="Zrly" SortExpression="Zrly">  
  47.                     <EditItemTemplate>  
  48.                         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Zrly") %>'></asp:TextBox>  
  49.                     </EditItemTemplate>  
  50.                     <ItemTemplate>  
  51.                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("Zrly") %>'></asp:Label>  
  52.                     </ItemTemplate>  
  53.                 </asp:TemplateField>  
  54.                 <asp:TemplateField HeaderText="Loco_req" SortExpression="Loco_req">  
  55.                     <EditItemTemplate>  
  56.                         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Loco_req") %>'></asp:TextBox>  
  57.                     </EditItemTemplate>  
  58.                     <ItemTemplate>  
  59.                         <asp:Label ID="Label2" runat="server" Text='<%# Bind("Loco_req") %>'></asp:Label>  
  60.                     </ItemTemplate>  
  61.                 </asp:TemplateField>  
  62.                 <asp:TemplateField HeaderText="Train" SortExpression="Train">  
  63.                     <EditItemTemplate>  
  64.                         <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Train") %>'></asp:TextBox>  
  65.                     </EditItemTemplate>  
  66.                     <ItemTemplate>  
  67.                         <asp:Label ID="Label3" runat="server" Text='<%# Bind("Train") %>'></asp:Label>  
  68.                     </ItemTemplate>  
  69.                 </asp:TemplateField>  
  70.                 <asp:TemplateField HeaderText="Origin" SortExpression="Origin">  
  71.                     <EditItemTemplate>  
  72.                         <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Origin") %>'></asp:TextBox>  
  73.                     </EditItemTemplate>  
  74.                     <ItemTemplate>  
  75.                         <asp:Label ID="Label4" runat="server" Text='<%# Bind("Origin") %>'></asp:Label>  
  76.                     </ItemTemplate>  
  77.                 </asp:TemplateField>  
  78.                 <asp:TemplateField HeaderText="Destin" SortExpression="Destin">  
  79.                     <EditItemTemplate>  
  80.                         <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Destin") %>'></asp:TextBox>  
  81.                     </EditItemTemplate>  
  82.                     <ItemTemplate>  
  83.                         <asp:Label ID="Label5" runat="server" Text='<%# Bind("Destin") %>'></asp:Label>  
  84.                     </ItemTemplate>  
  85.                 </asp:TemplateField>  
  86.                 <asp:TemplateField HeaderText="Rake_link" SortExpression="Rake_link">  
  87.                     <EditItemTemplate>  
  88.                         <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Rake_link") %>'></asp:TextBox>  
  89.                     </EditItemTemplate>  
  90.                     <ItemTemplate>  
  91.                         <asp:Label ID="Label6" runat="server" Text='<%# Bind("Rake_link") %>'></asp:Label>  
  92.                     </ItemTemplate>  
  93.                 </asp:TemplateField>  
  94.                 <asp:TemplateField HeaderText="Loco_link" SortExpression="Loco_link">  
  95.                     <EditItemTemplate>  
  96.                         <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Loco_link") %>'></asp:TextBox>  
  97.                     </EditItemTemplate>  
  98.                     <ItemTemplate>  
  99.                         <asp:Label ID="Label7" runat="server" Text='<%# Bind("Loco_link") %>'></asp:Label>  
  100.                     </ItemTemplate>  
  101.                 </asp:TemplateField>  
  102.                 <asp:TemplateField HeaderText="Saturday" SortExpression="Saturday">  
  103.                     <EditItemTemplate>  
  104.                         <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Saturday") %>'></asp:TextBox>  
  105.                     </EditItemTemplate>  
  106.                     <ItemTemplate>  
  107.                         <asp:Label ID="Label8" runat="server" Text='<%# Bind("Saturday") %>'></asp:Label>  
  108.                     </ItemTemplate>  
  109.                 </asp:TemplateField>  
  110.                 <asp:TemplateField HeaderText="Friday" SortExpression="Friday">  
  111.                     <EditItemTemplate>  
  112.                         <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Friday") %>'></asp:TextBox>  
  113.                     </EditItemTemplate>  
  114.                     <ItemTemplate>  
  115.                         <asp:Label ID="Label9" runat="server" Text='<%# Bind("Friday") %>'></asp:Label>  
  116.                     </ItemTemplate>  
  117.                 </asp:TemplateField>  
  118.                 <asp:TemplateField HeaderText="Thursday" SortExpression="Thursday">  
  119.                     <EditItemTemplate>  
  120.                         <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Thursday") %>'></asp:TextBox>  
  121.                     </EditItemTemplate>  
  122.                     <ItemTemplate>  
  123.                         <asp:Label ID="Label10" runat="server" Text='<%# Bind("Thursday") %>'></asp:Label>  
  124.                     </ItemTemplate>  
  125.                 </asp:TemplateField>  
  126.                 <asp:TemplateField HeaderText="Wednesday" SortExpression="Wednesday">  
  127.                     <EditItemTemplate>  
  128.                         <asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("Wednesday") %>'></asp:TextBox>  
  129.                     </EditItemTemplate>  
  130.                     <ItemTemplate>  
  131.                         <asp:Label ID="Label11" runat="server" Text='<%# Bind("Wednesday") %>'></asp:Label>  
  132.                     </ItemTemplate>  
  133.                 </asp:TemplateField>  
  134.                 <asp:TemplateField HeaderText="Tuesday" SortExpression="Tuesday">  
  135.                     <EditItemTemplate>  
  136.                         <asp:TextBox ID="TextBox12" runat="server" Text='<%# Bind("Tuesday") %>'></asp:TextBox>  
  137.                     </EditItemTemplate>  
  138.                     <ItemTemplate>  
  139.                         <asp:Label ID="Label12" runat="server" Text='<%# Bind("Tuesday") %>'></asp:Label>  
  140.                     </ItemTemplate>  
  141.                 </asp:TemplateField>  
  142.                 <asp:TemplateField HeaderText="Monday" SortExpression="Monday">  
  143.                     <EditItemTemplate>  
  144.                         <asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("Monday") %>'></asp:TextBox>  
  145.                     </EditItemTemplate>  
  146.                     <ItemTemplate>  
  147.                         <asp:Label ID="Label13" runat="server" Text='<%# Bind("Monday") %>'></asp:Label>  
  148.                     </ItemTemplate>  
  149.                 </asp:TemplateField>  
  150.                 <asp:TemplateField HeaderText="Sunday" SortExpression="Sunday">  
  151.                     <EditItemTemplate>  
  152.                         <asp:TextBox ID="TextBox14" runat="server" Text='<%# Bind("Sunday") %>'></asp:TextBox>  
  153.                     </EditItemTemplate>  
  154.                     <ItemTemplate>  
  155.                         <asp:Label ID="Label14" runat="server" Text='<%# Bind("Sunday") %>'></asp:Label>  
  156.                     </ItemTemplate>  
  157.                 </asp:TemplateField>  
  158.                 <asp:TemplateField HeaderText="Loco_no" SortExpression="Loco_no">  
  159.                     <EditItemTemplate>  
  160.                         <asp:TextBox ID="TextBox15" runat="server" Text='<%# Bind("Loco_no") %>'></asp:TextBox>  
  161.                     </EditItemTemplate>  
  162.                     <ItemTemplate>  
  163.                         <asp:Label ID="Label15" runat="server" Text='<%# Bind("Loco_no") %>'></asp:Label>  
  164.                     </ItemTemplate>  
  165.                 </asp:TemplateField>  
  166.                 <asp:TemplateField HeaderText="Loco_type" SortExpression="Loco_type">  
  167.                     <EditItemTemplate>  
  168.                         <asp:TextBox ID="TextBox16" runat="server" Text='<%# Bind("Loco_type") %>'></asp:TextBox>  
  169.                     </EditItemTemplate>  
  170.                     <ItemTemplate>  
  171.                         <asp:Label ID="Label16" runat="server" Text='<%# Bind("Loco_type") %>'></asp:Label>  
  172.                     </ItemTemplate>  
  173.                 </asp:TemplateField>  
  174.                 <asp:TemplateField HeaderText="updated_on" SortExpression="updated_on">  
  175.                     <EditItemTemplate>  
  176.                         <asp:TextBox ID="TextBox17" runat="server" Text='<%# Bind("updated_on") %>'></asp:TextBox>  
  177.                     </EditItemTemplate>  
  178.                     <ItemTemplate>  
  179.                         <asp:Label ID="Label17" runat="server" Text='<%# Bind("updated_on") %>'></asp:Label>  
  180.                     </ItemTemplate>  
  181.                 </asp:TemplateField>  
  182.                 <asp:CommandField ShowEditButton="True" />  
  183.             </Columns>  
  184.             <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />  
  185.             <RowStyle Font-Names="Tahoma" Font-Size="Small" HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />  
  186.         </asp:GridView>  
  187.         <asp:SqlDataSource ID="SqlDataSource1" runat="server"  CancelSelectOnNullParameter="false"  
  188.             ConnectionString="<%$ ConnectionStrings:Loco_bankConnectionString %>"   
  189.             DeleteCommand="DELETE FROM [Hotel_load] WHERE [ID] = @ID"   
  190.             InsertCommand="INSERT INTO [Hotel_load] ([Zrly], [Loco_req], [Train], [Origin], [Destin], [Rake_link], [Loco_link], [Saturday], [Friday], [Thursday], [Wednesday], [Tuesday], [Monday], [Sunday], [Loco_no], [Loco_type], [updated_on]) VALUES (@Zrly, @Loco_req, @Train, @Origin, @Destin, @Rake_link, @Loco_link, @Saturday, @Friday, @Thursday, @Wednesday, @Tuesday, @Monday, @Sunday, @Loco_no, @Loco_type, @updated_on)"   
  191.             SelectCommand="SELECT [ID], [Zrly], [Loco_req], [Train], [Origin], [Destin], [Rake_link], [Loco_link], [Saturday], [Friday], [Thursday], [Wednesday], [Tuesday], [Monday], [Sunday], [Loco_no], [Loco_type], [updated_on] FROM [Hotel_load] WHERE ([Saturday] = @Saturday OR @Saturday IS NULL) AND ([Sunday] = @Sunday OR @Sunday IS NULL) AND ([Monday] = @Monday OR @Monday IS NULL) AND ([Tuesday] = @Tuesday OR @Tuesday IS NULL) AND ([Wednesday] = @Wednesday OR @Wednesday IS NULL) AND ([Thursday] = @Thursday OR @Thursday IS NULL) AND ([Friday] = @Friday OR @Friday IS NULL) AND ([Zrly] = @Zrly) "   
  192.             UpdateCommand="UPDATE [Hotel_load] SET [Zrly] = @Zrly, [Loco_req] = @Loco_req, [Train] = @Train, [Origin] = @Origin, [Destin] = @Destin, [Rake_link] = @Rake_link, [Loco_link] = @Loco_link, [Saturday] = @Saturday, [Friday] = @Friday, [Thursday] = @Thursday, [Wednesday] = @Wednesday, [Tuesday] = @Tuesday, [Monday] = @Monday, [Sunday] = @Sunday, [Loco_no] = @Loco_no, [Loco_type] = @Loco_type, [updated_on] = @updated_on WHERE [ID] = @ID">  
  193.             <DeleteParameters>  
  194.                 <asp:Parameter Name="ID" Type="Int32" />  
  195.             </DeleteParameters>  
  196.             <InsertParameters>  
  197.                 <asp:Parameter Name="Zrly" Type="String" />  
  198.                 <asp:Parameter Name="Loco_req" Type="String" />  
  199.                 <asp:Parameter Name="Train" Type="String" />  
  200.                 <asp:Parameter Name="Origin" Type="String" />  
  201.                 <asp:Parameter Name="Destin" Type="String" />  
  202.                 <asp:Parameter Name="Rake_link" Type="String" />  
  203.                 <asp:Parameter Name="Loco_link" Type="String" />  
  204.                 <asp:Parameter Name="Saturday" Type="String" />  
  205.                 <asp:Parameter Name="Friday" Type="String" />  
  206.                 <asp:Parameter Name="Thursday" Type="String" />  
  207.                 <asp:Parameter Name="Wednesday" Type="String" />  
  208.                 <asp:Parameter Name="Tuesday" Type="String" />  
  209.                 <asp:Parameter Name="Monday" Type="String" />  
  210.                 <asp:Parameter Name="Sunday" Type="String" />  
  211.                 <asp:Parameter Name="Loco_no" Type="String" />  
  212.                 <asp:Parameter Name="Loco_type" Type="String" />  
  213.                 <asp:Parameter Name="updated_on" Type="DateTime" />  
  214.             </InsertParameters>  
  215.             <SelectParameters>  
  216.                 <asp:QueryStringParameter Name="Saturday" QueryStringField="Saturday" Type="String" />  
  217.                 <asp:QueryStringParameter Name="Sunday" QueryStringField="Sunday" Type="String" />  
  218.                 <asp:QueryStringParameter Name="Monday" QueryStringField="Monday" Type="String" />  
  219.                 <asp:QueryStringParameter Name="Tuesday" QueryStringField="Tuesday" Type="String" />  
  220.                 <asp:QueryStringParameter Name="Wednesday" QueryStringField="Wednesday" Type="String" />  
  221.                 <asp:QueryStringParameter Name="Thursday" QueryStringField="Thursday" Type="String" />  
  222.                 <asp:QueryStringParameter Name="Friday" QueryStringField="Friday" Type="String" />  
  223.                 <asp:QueryStringParameter Name="Zrly" QueryStringField="Zrly" Type="String" />  
  224.             </SelectParameters>  
  225.             <UpdateParameters>  
  226.                 <asp:Parameter Name="Zrly" Type="String" />  
  227.                 <asp:Parameter Name="Loco_req" Type="String" />  
  228.                 <asp:Parameter Name="Train" Type="String" />  
  229.                 <asp:Parameter Name="Origin" Type="String" />  
  230.                 <asp:Parameter Name="Destin" Type="String" />  
  231.                 <asp:Parameter Name="Rake_link" Type="String" />  
  232.                 <asp:Parameter Name="Loco_link" Type="String" />  
  233.                 <asp:Parameter Name="Saturday" Type="String" />  
  234.                 <asp:Parameter Name="Friday" Type="String" />  
  235.                 <asp:Parameter Name="Thursday" Type="String" />  
  236.                 <asp:Parameter Name="Wednesday" Type="String" />  
  237.                 <asp:Parameter Name="Tuesday" Type="String" />  
  238.                 <asp:Parameter Name="Monday" Type="String" />  
  239.                 <asp:Parameter Name="Sunday" Type="String" />  
  240.                 <asp:Parameter Name="Loco_no" Type="String" />  
  241.                 <asp:Parameter Name="Loco_type" Type="String" />  
  242.                 <asp:Parameter Name="updated_on" Type="DateTime" />  
  243.                 <asp:Parameter Name="ID" Type="Int32" />  
  244.             </UpdateParameters>  
  245.         </asp:SqlDataSource>  
  246.     </form>  
  247. </body>  
  248. </html>  
 


  
 

Answers (1)