Get Selectparamater default value for dropdownlist

gabi456
gabi456 used Ask the Experts™
on
Hello

I need to set the @MivjanID parameter on the ID="DropDownListNormot" to the mivjan id of the row that i have clicked the edit linkbutton on.

Please help.

<asp:GridView ID="GridViewErcaMivchan" AllowSorting="true" SkinID="Professional"
        EmptyDataText="fgdfg" runat="server" AutoGenerateColumns="False"
        DataKeyNames="ErcaID,MivjanID" DataSourceID="SqlDataSourceErcaMivchan">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="âèÚ"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="True" CommandName="Update"
                        Text="âÓÛß"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="ÑØÜ"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                        OnClientClick="return confirm('ÜÔáÙè Ðê ÔéÕèÔ?');" Text="Ôáè"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ErcaID" HeaderText="Þáäè âèÛÔ" ReadOnly="True" />
            <asp:BoundField DataField="Erca" HeaderText="âèÛÔ" ReadOnly="True" />
            <asp:BoundField DataField="MivjanID" HeaderText="MivjanID" SortExpression="MivjanID"
                ReadOnly="True" />
            <asp:BoundField DataField="Mivjan" HeaderText="ÞÑ×ß" SortExpression="Mivjan" ReadOnly="True" />
            <asp:BoundField DataField="NormaID" HeaderText="NormaID" SortExpression="NormaID"
                ReadOnly="True" />
            <asp:TemplateField HeaderText="àÕèÞÔ" SortExpression="Norma">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Norma") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList SelectedValue='<%# Bind("NormaID") %>' ID="DropDownListNormot"
                        CssClass="DropDown" runat="server" DataSourceID="SqlDataSourceNormot" DataTextField="Norma"
                        DataValueField="NormaID" AutoPostBack="False">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourceNormot" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                        SelectCommand="SELECT DISTINCT Normot.NormaID, Normot.Norma
                                        FROM         Normot INNER JOIN
                                                              Mafteajtziunim ON Normot.NormaID = Mafteajtziunim.NormaID
                                        WHERE     (Mafteajtziunim.MivjanID = @MivjanID)
                                        ORDER BY Normot.Norma">
                        <SelectParameters>
                            <asp:Parameter Name="MivjanID" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Use on RowDataBound event of GridView.

in that event
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit )
 {
    DropdownList lst = e.Row.FindControl("DropDownListNormot");
    // bind value over here.
}

Commented:
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Edit  Then

    SqlDataSource ds = e.Row.FindControl("SqlDataSourceNormot") as SqlDataSource;
   ds.SelectParameters("MivjanID").Value = // set value over here.
 
End If
 
 

Author

Commented:
How do i get the MivjanID as the selected item in the dropdown?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Commented:
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Edit  Then

   SqlDataSource ds = e.Row.FindControl("SqlDataSourceNormot") as SqlDataSource;
   ds.SelectParameters("MivjanID").Value = Integer.Parse(DataBinder.Eval(e.Row.DataItem , "MivjanID").ToString());

End If

Is this you want ?

In order to set dropdown value you just need to set control.

put code snipper if possible.

Author

Commented:
When i press the update linkbutton i get an error:Must declare the scalar variable "@NormaID".

What must i add for the update to work when making a change in the dropdown list?
Server Error in '/' Application.
--------------------------------------------------------------------------------
 
Must declare the scalar variable "@NormaID". 
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.Data.SqlClient.SqlException: Must declare the scalar variable "@NormaID".
 
Source Error: 

Open in new window

Commented:
what is declaration of SqlDataSourceErcaMivchan in ASPX ?

IS that missing any parameter ?

Author

Commented:
Hello Jinal

I have made some changes to the code and now am getting a new error.

Please help.

Server Error in '/' Application.
--------------------------------------------------------------------------------

Could not find control 'DropDownListNormot' in ControlParameter 'NormaID'.
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.InvalidOperationException: Could not find control 'DropDownListNormot' in ControlParameter 'NormaID'.

<%@ Page Title="" Language="VB" MasterPageFile="~/ExamAdmin/AdminMasterPage.master"
    AutoEventWireup="false" CodeFile="ErcotMivchanim.aspx.vb" Inherits="ExamAdmin_Ercot_ErcotMivchanim" %>
 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <h1>
        ÔÕáäê ÞÑ×àÙÝ ÜäÙ êÛÕàÕê ÜâèÛÔ</h1>
    <table>
        <tr>
            <td valign="top" style="width: 250px">
                <b>âèÛÕê</b>
                <br />
                <asp:DropDownList ID="DropDownListErcotCats" CssClass="DropDown" runat="server" DataSourceID="SqlDataSourceErcotCats"
                    DataTextField="ErcaCat" DataValueField="ErcaCatID" AutoPostBack="True">
                </asp:DropDownList>
                <br />
                <asp:SqlDataSource ID="SqlDataSourceErcotCats" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                    SelectCommand="SELECT [ErcaCatID], [LangID], [ErcaCat] FROM [LangErcaCats] WHERE ([LangID] = @LangID) ORDER BY [ErcaCatID]">
                    <SelectParameters>
                        <asp:SessionParameter Name="LangID" SessionField="LangID" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:DropDownList ID="DropDownListErcot" AutoPostBack="true" CssClass="DropDown"
                    runat="server" DataSourceID="SqlDataSourceErcot" DataTextField="Erca" DataValueField="ErcaID">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSourceErcot" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                    SelectCommand="SELECT LangErcaAdmin.Erca, LangErcaAdmin.ErcaID, ErcaCatsErcot.ErcaCatID FROM LangErcaAdmin INNER JOIN ErcaCatsErcot ON LangErcaAdmin.ErcaID = ErcaCatsErcot.ErcaID WHERE (LangErcaAdmin.LangAdminID = @LangAdminID) AND (ErcaCatsErcot.ErcaCatID = @ErcaCatID)">
                    <SelectParameters>
                        <asp:SessionParameter Name="LangAdminID" SessionField="LangID" Type="Int32" />
                        <asp:ControlParameter ControlID="DropDownListErcotCats" Name="ErcaCatID" PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <a href="../Ercot.aspx">ÔÕáã âèÛÔ ×ÓéÔ</a>
                <br />
                <br />
                <b>êÛÕàÕê</b><br />
                <asp:DropDownList ID="ListBoxTchunot" CssClass="DropDown" runat="server" DataSourceID="SqlDataSourceTchunotList"
                    DataTextField="TchunaWithID" DataValueField="TchunaID" AutoPostBack="True">
                </asp:DropDownList>
                <a href="../Tchunot/Tchunot.aspx">ÔÕáã êÛÕàÔ ×ÓéÔ</a>
                <asp:SqlDataSource ID="SqlDataSourceTchunotList" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                    SelectCommand="SELECT [Tchuna], [TchunaID] , Tchuna + ' (' + CAST(TchunaID AS char(3))  + ')' AS TchunaWithID  FROM [LangTchunot] WHERE ([LangID] = @LangID) order by tchuna">
                    <SelectParameters>
                        <asp:SessionParameter Name="LangID" SessionField="LangID" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <br />
                <br />
                <asp:Label ID="LabelNoMoreExams" runat="server" ForeColor="Red" Text="*** ÐÙÕ âÕÓ ÞÑ×àÙÝ ÜÔÕáÙã ***<br />"
                    Visible="false"></asp:Label>
                <b>ÞÑ×àÙÝ</b><br />
                <asp:ListBox ID="ListBoxMivchanimList" Width="205px" SelectionMode="Single" Rows="10"
                    AutoPostBack="true" CssClass="DropDown" runat="server" DataSourceID="SqlDataSourceMivchanimList"
                    DataTextField="Mivjan" DataValueField="MivjanID"></asp:ListBox>
                <asp:SqlDataSource ID="SqlDataSourceMivchanimList" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                    SelectCommand="SELECT     dbo.LangMivjanimAdmin.Mivjan, dbo.TchunaMivjan.TchunaID, dbo.Mivjanim.MivjanID
                                    FROM         dbo.LangMivjanimAdmin INNER JOIN
                                                          dbo.Mivjanim ON dbo.LangMivjanimAdmin.MivjanID = dbo.Mivjanim.MivjanID INNER JOIN
                                                          dbo.TchunaMivjan ON dbo.Mivjanim.MivjanID = dbo.TchunaMivjan.MivjanID
                                    WHERE     (dbo.TchunaMivjan.TchunaID = @TchunaID) AND (dbo.LangMivjanimAdmin.LangAdminID = @LangID)
                                    
                                    
                                    
                                   AND dbo.Mivjanim.MivjanID NOT IN 
 
                                (
                                SELECT     dbo.ErcaMivjan.MivjanID
                                FROM         dbo.ErcaMivjan INNER JOIN
                                dbo.LangMivjanimAdmin ON dbo.ErcaMivjan.MivjanID = dbo.LangMivjanimAdmin.MivjanID
                                WHERE     (dbo.ErcaMivjan.ErcaID = @ErcaID)
                                )">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ListBoxTchunot" Name="TchunaID" PropertyName="SelectedValue" />
                        <asp:SessionParameter Name="LangID" SessionField="LangID" />
                        <asp:ControlParameter ControlID="DropDownListErcot" Name="ErcaID" PropertyName="SelectedValue"
                            Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
            <td style="width: 20px">
            </td>
            <td valign="top">
                <h2>
                    áÓè éÜ ÔÞÑ×àÙÝ</h2>
                <div class="reorderListDemo">
                    <cc1:ReorderList ID="ReorderListErcotMivchan" LayoutType="Table" Width="300px" CallbackCssStyle="callbackStyle"
                        runat="server" DataSourceID="SqlDataSourceRe" DragHandleAlignment="Left" ItemInsertLocation="Beginning"
                        DataKeyField="MivjanID" SortOrderField="Seder" AllowReorder="true" PostBackOnReorder="True">
                        <ItemTemplate>
                            <div class="itemArea">
                                <strong>
                                    <asp:Label ID="lblRuleIndex" runat="server" Text='<%# Eval("Mivjan") %>' /></strong>
                                &nbsp;<asp:Label ID="lblRuleName" runat="server" Text='<%# Eval("MivjanID") %>' />
                            </div>
                        </ItemTemplate>
                        <ReorderTemplate>
                            <asp:Panel ID="Panel2" runat="server" CssClass="reorderCue" />
                        </ReorderTemplate>
                        <DragHandleTemplate>
                            <div class="dragHandle">
                            </div>
                        </DragHandleTemplate>
                    </cc1:ReorderList>
                </div>
                <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                    ID="SqlDataSourcere" runat="server" SelectCommand="SELECT     ErcaMivjan.ErcaID, LangMivjanimAdmin.Mivjan, ErcaMivjan.MivjanID, ErcaMivjan.Seder
                            FROM         ErcaMivjan INNER JOIN
                                                  LangMivjanimAdmin ON ErcaMivjan.MivjanID = LangMivjanimAdmin.MivjanID
                            WHERE     (ErcaMivjan.ErcaID = @ErcaID) AND (dbo.LangMivjanimAdmin.LangAdminID = @LangID) order by Seder ASC, DateAdded"
                    UpdateCommand="UPDATE [ErcaMivjan] SET [Seder] = @Seder WHERE [MivjanID] = @original_MivjanID"
                    OldValuesParameterFormatString="original_{0}">
                    <SelectParameters>
                        <asp:SessionParameter Name="LangID" SessionField="LangID" Type="Int32" />
                        <asp:ControlParameter ControlID="DropDownListErcot" Name="ErcaID" PropertyName="SelectedValue"
                            Type="Int32" />
                    </SelectParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Seder" Type="Int32" />
                        <asp:Parameter Name="original_MivjanID" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </table>
    <br />
    <h2>
        ÞÑ×àÙÞ ÑâèÛÔ
        <asp:Label ID="ErcaNameLabel" runat="server"></asp:Label></h2>
    <asp:GridView ID="GridViewErcaMivchan" AllowSorting="true" SkinID="Professional"
        EmptyDataText="ÐÙÕ ÞÑ×àÙÝ ÑâèÛÔ ÖÐê" runat="server" AutoGenerateColumns="False"
        DataKeyNames="ErcaID,MivjanID" DataSourceID="SqlDataSourceErcaMivchan">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="âèÚ"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="True" CommandName="Update"
                        Text="âÓÛß"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="ÑØÜ"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                        OnClientClick="return confirm('ÜÔáÙè Ðê ÔéÕèÔ?');" Text="Ôáè"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ErcaID" HeaderText="Þáäè âèÛÔ" ReadOnly="True" />
            <asp:BoundField DataField="Erca" HeaderText="âèÛÔ" ReadOnly="True" />
            <asp:BoundField DataField="MivjanID" HeaderText="MivjanID" SortExpression="MivjanID"
                ReadOnly="True" />
            <asp:BoundField DataField="Mivjan" HeaderText="ÞÑ×ß" SortExpression="Mivjan" ReadOnly="True" />
            <asp:BoundField DataField="NormaID" HeaderText="NormaID" SortExpression="NormaID"
                ReadOnly="True" />
            <asp:TemplateField HeaderText="àÕèÞÔ" SortExpression="Norma">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Norma") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownListNormot" CssClass="DropDown" runat="server" DataSourceID="SqlDataSourceNormot"
                        DataTextField="Norma" DataValueField="NormaID" AutoPostBack="False">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourceNormot" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
                        SelectCommand="SELECT DISTINCT Normot.NormaID, Normot.Norma
                                        FROM         Normot INNER JOIN
                                                              Mafteajtziunim ON Normot.NormaID = Mafteajtziunim.NormaID
                                        WHERE     (Mafteajtziunim.MivjanID = @MivjanID)
                                        ORDER BY Normot.Norma">
                        <SelectParameters>
                            <asp:Parameter Name="MivjanID" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSourceErcaMivchan" runat="server" ConnectionString="<%$ ConnectionStrings:MeitavNetConnectionString %>"
        SelectCommand="SELECT dbo.ErcaMivjan.ErcaID, dbo.LangMivjanimAdmin.Mivjan, dbo.ErcaMivjan.MivjanID, dbo.LangErcaAdmin.Erca, dbo.Normot.Norma, dbo.Normot.NormaID
                                    FROM         dbo.ErcaMivjan INNER JOIN
                                                          dbo.LangMivjanimAdmin ON dbo.ErcaMivjan.MivjanID = dbo.LangMivjanimAdmin.MivjanID INNER JOIN
                                                          dbo.LangErcaAdmin ON dbo.ErcaMivjan.ErcaID = dbo.LangErcaAdmin.ErcaID INNER JOIN
                                                          dbo.Normot ON dbo.ErcaMivjan.NormaID = dbo.Normot.NormaID
                                    WHERE     (dbo.LangMivjanimAdmin.LangAdminID = @LangID) AND (dbo.ErcaMivjan.ErcaID = @ErcaID) AND (dbo.LangErcaAdmin.LangAdminID = @LangID)
                                    ORDER BY dbo.ErcaMivjan.Seder, dbo.ErcaMivjan.DateAdded"
        DeleteCommand="DELETE FROM ErcaMivjan WHERE (ErcaID = @SelectedErcaID and MivjanID = @MivjanID)"
        ProviderName="System.Data.SqlClient" UpdateCommand="UPDATE ErcaMivjan SET NormaID = @NormaID where ErcaID = @ErcaID and MivjanID = @MivjanID">
        <SelectParameters>
            <asp:SessionParameter Name="LangID" SessionField="LangID" />
            <asp:ControlParameter ControlID="DropDownListErcot" Name="ErcaID" PropertyName="SelectedValue"
                Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:ControlParameter ControlID="DropDownListNormot" Name="NormaID" PropertyName="SelectedValue"
                Type="Int32" />
        </UpdateParameters>
        <DeleteParameters>
            <asp:ControlParameter ControlID="DropDownListErcot" Name="SelectedErcaID" PropertyName="SelectedValue"
                Type="Int32" />
            <asp:Parameter Name="MivjanID" Type="Int32" />
        </DeleteParameters>
    </asp:SqlDataSource>
</asp:Content>

Open in new window

Commented:
<UpdateParameters>
            <asp:ControlParameter ControlID="DropDownListNormot" Name="NormaID" PropertyName="SelectedValue"
                Type="Int32" />
        </UpdateParameters>
DropDownListNormot control inside gridview template you can not use outside .  
 

Author

Commented:
I have a new problem, managed to get this far:

Don't worry will sort out points later :)

Server Error in '/' Application.
--------------------------------------------------------------------------------

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.

Source Error:


Line 76:
Line 77:     Protected Sub SqlDataSourceErcaMivchan_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSourceErcaMivchan.Updating
Line 78:         If GridViewErcaMivchan.SelectedRow.RowState = DataControlRowState.Edit Then
Line 79:             Dim newname As DropDownList = GridViewErcaMivchan.FindControl("DropDownListNormot")
Line 80:             e.Command.Parameters("@NormaID").Value = newname.SelectedValue
 

Source File: D:\websites\MeitavNet\ExamAdmin\Ercot\ErcotMivchanim.aspx.vb    Line: 78

Protected Sub SqlDataSourceErcaMivchan_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSourceErcaMivchan.Updating
        If GridViewErcaMivchan.SelectedRow.RowState = DataControlRowState.Edit Then
            Dim newname As DropDownList = GridViewErcaMivchan.FindControl("DropDownListNormot")
            e.Command.Parameters("@NormaID").Value = newname.SelectedValue
 
        End If
 
 
    End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial