0

Hi

I'm trying to use a dropdownlist in the DetailsView, but when I click the insert button I see in the table that the field Maker just contain NULL value. I haven't been using the GridView or DetailsView that much so I need help. Is there a way to insert from code beind when user clicks the btnAdd?

The aspx code

<asp:GridView ID="grdCam" ShowFooter ="true"  runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" CellPadding="4" DataKeyNames="CamID" EmptyDataText="There are no data records to display."
ForeColor="#333333" GridLines="None" OnRowCommand ="grdCam_RowCommand" OnRowEditing="grdCam_RowEditing" OnRowDeleting="grdCam_RowDeleting" OnRowUpdating="grdCam_RowUpdating">
<FooterStyle  BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="CamID" SortExpression="CamID">
<FooterTemplate>
<asp:Button  ID="btnAdd" runat="server" CommandName="Insert" Text="Add" />
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblCamID" runat="server" Text='<%# Bind("CamID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kamera" SortExpression="CamName">
<EditItemTemplate>
<asp:TextBox ID="txtKam" runat="server" Text='<%# Bind("CamName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("CamName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tillverkare" SortExpression="user_first">
<EditItemTemplate>
<asp:SqlDataSource SelectCommand="Select * from Maker" ConnectionString="<%$ ConnectionStrings:CamConnString %>" ID="SqlDSMaker" runat="server"></asp:SqlDataSource>
<asp:DropDownList ID="ddlMaker" DataValueField="MakerID" DataSourceID="SqlDSMaker"
DataTextField="MakerName" AutoPostBack="True" runat="server" Text='<%# Bind("MakerID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("MakerName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Megapixel" SortExpression="MegaPix">
<EditItemTemplate>
<asp:TextBox ID="txtMPix" runat="server" Text='<%# Bind("MegaPix") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("MegaPix") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pris" SortExpression="Price">
<EditItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Antal" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtAntal" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:CamConnString %>"
InsertCommand="insert into DigCameras(CamName,Maker,MegaPix,Price,Quantity)values(@CamName,@MakerID,@MegaPix,@Price,@Quantity)">
<InsertParameters>
<asp:Parameter Name="CamName" Type="String" />
<asp:Parameter Name="MakerID" Type="String" />
<asp:Parameter Name="MegaPix" Type="String" />
<asp:Parameter Name="Price" Type="String" />
<asp:Parameter Name="Quantity" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
&nbsp;<asp:DetailsView ID="dvwCam" runat="server" DataSourceID="SqlDataSource1" AutoGenerateRows="False" CellPadding="4" DataKeyNames="CustomerID" DefaultMode="Insert" ForeColor="#333333" GridLines="None" Height="50px" Width="100%" OnItemInserted="dvwCam_ItemInserted">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="CamName" HeaderText="Kamera :" SortExpression="CamName" />
<asp:TemplateField HeaderText="Tillverkare :">
<EditItemTemplate>
<asp:SqlDataSource SelectCommand="Select * from Maker" ConnectionString="<%$ ConnectionStrings:CamConnString %>" ID="SqlDSMaker" runat="server"></asp:SqlDataSource>
<asp:DropDownList ID="ddlMaker1" DataSourceID="SqlDSMaker" runat="server" DataTextField="MakerName" DataValueField="MakerID" AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MegaPix" HeaderText="Megapixel :" SortExpression="MegaPix" />
<asp:BoundField DataField="Price" HeaderText="Pris :" SortExpression="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Antal :" SortExpression="Quantity" />
<asp:CommandField ShowInsertButton="True"  />
</Fields>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" HorizontalAlign="Right" VerticalAlign="Top" Width="10%" Wrap="False" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</EmptyDataTemplate>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

The code behind

protected void dvwCam_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        BindGrid();
    }

    protected void grdCam_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            grdCam.DataSourceID = "";
            grdCam.DataBind();
        }
    }

    protected void BindGrid()
    {
        Repeater rpt;
        //DetailsView dvwCam = (DetailsView)grdCam.FindControl("dvwCam");
       //string str="insert into DigCameras(CamName,Maker,MegaPix,Price,Quantity)values(@CamName,@MakerID,@MegaPix,@Price,@Quantity) ";
        SqlCommand cmd = new SqlCommand("ShowCameras", conn);
        //SqlCommand cmd1 = new SqlCommand(str, conn);

        cmd.CommandType = CommandType.StoredProcedure;
        conn.Open();
        cmd.ExecuteNonQuery();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        //da.InsertCommand = cmd1;
        da.Fill(ds, "tblShow");
        rpt = (Repeater)Master.FindControl("rptShow");
        rpt.DataSource = ds;
        rpt.DataBind();
        grdCam.DataSource = ds.Tables["tblShow"].DefaultView;
        grdCam.DataBind();
       
        //dvwCam.DataSource = ds.Tables["tblShow"];
        //dvwCam.DataBind();
        conn.Close();
    }

Please help
Fia

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

>when I click the insert button I see in the table that the field Maker just contain NULL value.

Where is InsertItemTemplate?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.