Can someone please tell me how to refrence the Details2SqlDataSource1 in codebehind so I can do:
Details2SqlDataSource1.InsertParameters(
"VendorCode").DefaultValue = Request.QueryString("VendorCode")Here's my code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <table style="width:100%;"> <tr> <td> </td> </tr> <tr> <td> <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="VendorServicesCode" DataSourceID="T1SqlDataSource1" style="font-family: Verdana; font-size: x-small"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" SelectText="+" DeleteText="-" > <ItemStyle VerticalAlign="Top" /> </asp:CommandField> <asp:TemplateField HeaderText="Service" SortExpression="CategoryCode"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CategoryCode") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryCode") %>' Visible="False"></asp:Label> <asp:Repeater ID="Repeater5" runat="server" DataSourceID="CatSqlDataSource1"> <ItemTemplate> <%#Eval("CategoryName")%> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="CatSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" SelectCommand="SELECT * FROM [CategoryMst] WHERE ([CategoryCode] = @CategoryCode)"> <SelectParameters> <asp:ControlParameter ControlID="Label1" Name="CategoryCode" PropertyName="Text" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" DataKeyNames="VendorProductsCode" DataSourceID="T2SqlDataSource1" GridLines="None" ShowHeader="False" Width="150px"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" SelectText="+" DeleteText="-" /> <asp:TemplateField HeaderText="Product" SortExpression="ProductCode"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductCode") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductCode") %>' Visible="False"></asp:Label> <asp:Repeater ID="Repeater4" runat="server" DataSourceID="ProdLabelSqlDataSource1"> <ItemTemplate> <%# Eval("ProductName") %> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="ProdLabelSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" SelectCommand="SELECT * FROM [ProductMst] WHERE ([ProductCode] = @ProductCode)"> <SelectParameters> <asp:ControlParameter ControlID="Label1" Name="ProductCode" PropertyName="Text" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </ItemTemplate> <ItemStyle Width="125px" /> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="T2SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" DeleteCommand="DELETE FROM [VendorProducts] WHERE [VendorProductsCode] = @VendorProductsCode" InsertCommand="INSERT INTO [VendorProducts] ([VendorLocationCode], [VendorCode], [VendorRegionCode], [CategoryCode], [ProductCode]) VALUES (@VendorLocationCode, @VendorCode, @VendorRegionCode, @CategoryCode, @ProductCode)" SelectCommand="SELECT * FROM [VendorProducts] WHERE (([VendorCode] = @VendorCode) AND ([VendorLocationCode] = @VendorLocationCode) AND ([CategoryCode] = @CategoryCode))" UpdateCommand="UPDATE [VendorProducts] SET [VendorLocationCode] = @VendorLocationCode, [VendorCode] = @VendorCode, [VendorRegionCode] = @VendorRegionCode, [CategoryCode] = @CategoryCode, [ProductCode] = @ProductCode WHERE [VendorProductsCode] = @VendorProductsCode"> <SelectParameters> <asp:QueryStringParameter Name="VendorCode" QueryStringField="VendorCode" Type="Int32" /> <asp:QueryStringParameter Name="VendorLocationCode" QueryStringField="VendorLocationCode" Type="Int32" /> <asp:ControlParameter ControlID="Label1" Name="CategoryCode" PropertyName="Text" Type="Int32" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="VendorProductsCode" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> <asp:Parameter Name="ProductCode" Type="Int32" /> <asp:Parameter Name="VendorProductsCode" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> <asp:Parameter Name="ProductCode" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> <br /> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="VendorProductsCode" DataSourceID="Details2SqlDataSource1" Height="50px" Width="125px"> <Fields> <asp:TemplateField HeaderText="Product" SortExpression="ProductCode"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductCode") %>'></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ProdDDLSqlDataSource1" DataTextField="ProductName" DataValueField="ProductCode" SelectedValue='<%# Bind("ProductCode") %>'> </asp:DropDownList> <asp:SqlDataSource ID="ProdDDLSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" SelectCommand="SELECT * FROM [ProductMst] WHERE ([CategoryCode] = @CategoryCode) ORDER BY [ProductName]"> <SelectParameters> <asp:ControlParameter ControlID="Label1" Name="CategoryCode" PropertyName="Text" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductCode") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowInsertButton="True" /> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="Details2SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" DeleteCommand="DELETE FROM [VendorProducts] WHERE [VendorProductsCode] = @VendorProductsCode" InsertCommand="INSERT INTO [VendorProducts] ([VendorLocationCode], [VendorCode], [VendorRegionCode], [CategoryCode], [ProductCode]) VALUES (@VendorLocationCode, @VendorCode, @VendorRegionCode, @CategoryCode, @ProductCode)" SelectCommand="SELECT * FROM [VendorProducts] WHERE ([VendorProductsCode] = @VendorProductsCode)" UpdateCommand="UPDATE [VendorProducts] SET [VendorLocationCode] = @VendorLocationCode, [VendorCode] = @VendorCode, [VendorRegionCode] = @VendorRegionCode, [CategoryCode] = @CategoryCode, [ProductCode] = @ProductCode WHERE [VendorProductsCode] = @VendorProductsCode"> <SelectParameters> <asp:ControlParameter ControlID="GridView4" Name="VendorProductsCode" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="VendorProductsCode" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> <asp:Parameter Name="ProductCode" Type="Int32" /> <asp:Parameter Name="VendorProductsCode" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> <asp:Parameter Name="ProductCode" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="T1SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" DeleteCommand="DELETE FROM [VendorServices] WHERE [VendorServicesCode] = @VendorServicesCode" InsertCommand="INSERT INTO [VendorServices] ([VendorLocationCode], [VendorRegionCode], [VendorCode], [CategoryCode]) VALUES (@VendorLocationCode, @VendorRegionCode, @VendorCode, @CategoryCode)" SelectCommand="SELECT * FROM [VendorServices] WHERE (([VendorCode] = @VendorCode) AND ([VendorLocationCode] = @VendorLocationCode))" UpdateCommand="UPDATE [VendorServices] SET [VendorLocationCode] = @VendorLocationCode, [VendorRegionCode] = @VendorRegionCode, [VendorCode] = @VendorCode, [CategoryCode] = @CategoryCode WHERE [VendorServicesCode] = @VendorServicesCode"> <SelectParameters> <asp:QueryStringParameter Name="VendorCode" QueryStringField="VendorCode" Type="Int32" /> <asp:QueryStringParameter Name="VendorLocationCode" QueryStringField="VendorLocationCode" Type="Int32" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="VendorServicesCode" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> <asp:Parameter Name="VendorServicesCode" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> <asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" DataKeyNames="VendorServicesCode" DataSourceID="DetailsSqlDataSource1" Height="50px" Width="125px" style="font-family: Verdana; font-size: x-small"> <Fields> <asp:TemplateField HeaderText="Category" SortExpression="CategoryCode"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ServicesDDLSqlDataSource1" DataTextField="CategoryName" DataValueField="CategoryCode" SelectedValue='<%# Bind("CategoryCode") %>'> </asp:DropDownList> <asp:SqlDataSource ID="ServicesDDLSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" SelectCommand="SELECT [CategoryName], [CategoryCode] FROM [CategoryMst] ORDER BY [CategoryName]"> </asp:SqlDataSource> </EditItemTemplate> <InsertItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ServicesDDLSqlDataSource1" DataTextField="CategoryName" DataValueField="CategoryCode" SelectedValue='<%# Bind("CategoryCode") %>'> </asp:DropDownList> <asp:SqlDataSource ID="ServicesDDLSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" SelectCommand="SELECT [CategoryName], [CategoryCode] FROM [CategoryMst] ORDER BY [CategoryName]"> </asp:SqlDataSource> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryCode") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowInsertButton="True" /> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="DetailsSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SiteDataConnStr %>" DeleteCommand="DELETE FROM [VendorServices] WHERE [VendorServicesCode] = @VendorServicesCode" InsertCommand="INSERT INTO [VendorServices] ([VendorLocationCode], [VendorRegionCode], [VendorCode], [CategoryCode]) VALUES (@VendorLocationCode, @VendorRegionCode, @VendorCode, @CategoryCode)" SelectCommand="SELECT * FROM [VendorServices] WHERE (([VendorCode] = @VendorCode) AND ([VendorLocationCode] = @VendorLocationCode) AND ([VendorServicesCode] = @VendorServicesCode))" UpdateCommand="UPDATE [VendorServices] SET [VendorLocationCode] = @VendorLocationCode, [VendorRegionCode] = @VendorRegionCode, [VendorCode] = @VendorCode, [CategoryCode] = @CategoryCode WHERE [VendorServicesCode] = @VendorServicesCode"> <SelectParameters> <asp:QueryStringParameter Name="VendorCode" QueryStringField="VendorCode" Type="Int32" /> <asp:QueryStringParameter Name="VendorLocationCode" QueryStringField="VendorLocationCode" Type="Int32" /> <asp:ControlParameter ControlID="GridView3" Name="VendorServicesCode" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="VendorServicesCode" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> <asp:Parameter Name="VendorServicesCode" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="VendorLocationCode" Type="Int32" /> <asp:Parameter Name="VendorRegionCode" Type="Int32" /> <asp:Parameter Name="VendorCode" Type="Int32" /> <asp:Parameter Name="CategoryCode" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> <br /> </td> </tr> </table> </asp:Content>
![]() |
0 |
![]() |
Object reference not set to an instance of an object.
Line 31:
Line 32: Dim Details2SqlDataSource1 As SqlDataSource
Line 33: Details2SqlDataSource1 = GridView3.SelectedRow.FindControl("Details2SqlDataSource1")
[NullReferenceException: Object reference not set to an instance of an object.]
test.Page_Load(Object sender, EventArgs e) in C:\websites\telegent\test.aspx.vb:33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
![]() |
0 |
![]() |
I am simply trying to access the sqlquery so I can then do this:
Details2SqlDataSource1.InsertParameters("VendorCode").DefaultValue = Request.QueryString("VendorCode")Details2SqlDataSource1.InsertParameters(
"VendorLocationCode").DefaultValue = Request.QueryString("VendorLocationCode")Details2SqlDataSource1.InsertParameters(
"CategoryCode").DefaultValue = Label1.Text
![]() |
0 |
![]() |
The query is located within Gridview3. I can't access it directly.
![]() |
0 |
![]() |
The datasource I am trying to get to is Details2SqlDataSource1 which is bound to a DetailsView, and both the detailsview and the Details2SqlDataSource1 are inside the Gridview3 gridview.
When I try to set it in pageload with simply:
Details2SqlDataSource1.InsertParameters("VendorCode").DefaultValue = Request.QueryString("VendorCode"), it says Details2SqlDataSource1 is not declared. That makes me think that I need to do a findcontrol on it.
![]() |
0 |
![]() |
Nope, didn't work.
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.
![]() |
0 |
![]() |
I have tried Page Load, and GridView3_RowDataBound, and GridView3_Load
![]() |
0 |
![]() |
try this in Page_Load
try this
Dim ds As sqldatasource = TryCast(GridView3.FindControl("Details2SqlDataSource1"), sqldatasource)
ds.InsertParameters("VendorCode").DefaultValue = Request.QueryString("VendorCode")
Selectedrow probably doesn't make sense while loading.
Regards
Partha Mandayam
Sr Prog Analyst
Kaiser Permanente
![]() |
0 |
![]() |
Object reference not set to an instance of an object
![]() |
0 |
![]() |
Hi jfierst ,
Try to invoke findcontrol method in GridView3_RowDataBound and you need to do it in the rows which RowType equals DataRow.
See my codes below.
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//do findcontrol
}
}
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
![]() |
0 |
![]() |