Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using visual studio to edit two tables in SQL Server.

The problem I'm facing is when updating something that uses the DopDownList, the update command sends null values in place of ok.

Can anyone help me?

I send My code to see if there is someone who can help.


ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MaterialRecebido.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style2 {
            width: 322px;
            height: 26px;
        }
        .auto-style4 {
            width: 55px;
            height: 26px;
        }
        .auto-style10 {
            width: 55px;
            height: 27px;
        }
        .auto-style11 {
            width: 143px;
            height: 27px;
        }
        .auto-style12 {
            width: 143px;
            height: 26px;
        }
        .auto-style13 {
            width: 91px;
            height: 27px;
        }
        .auto-style14 {
            width: 91px;
            height: 26px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Material Recebido" BackColor="#FFFF66" Font-Size="XX-Large" Font-Bold="True"></asp:Label>
        </div>
         <table style="width:100%;">
            <tr>
                <td class="auto-style11"><asp:Label ID="Label2" runat="server" Text="Nº quem recebeu " Visible="False"></asp:Label>
                </td>
                <td class="auto-style13">
        <asp:TextBox ID="TextBox1" runat="server" Width="85px" Visible="False"></asp:TextBox>
                </td>
                <td class="auto-style10"></td>
            </tr>
            <tr>
                <td class="auto-style12">
                    <asp:Label ID="Label3" runat="server" Text="Fornecedor "></asp:Label>
                </td>
                <td class="auto-style14">
                    <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" Width="174px"></asp:TextBox>
                </td>
                <td class="auto-style4">
                     </td>
                        <td class="auto-style2">
                             </td>
            </tr>
             
            
               
        </table>

        <hr />
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="bistamp" DataSourceID="SqlDataSource1" CellPadding="4" DataMember="DefaultView" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="bistamp" HeaderText="bistamp" ReadOnly="True" SortExpression="bistamp" Visible="False" />
                <asp:BoundField DataField="obra" HeaderText="obra" ReadOnly="True" SortExpression="obra" />
                <asp:BoundField DataField="fornecedor" HeaderText="fornecedor" ReadOnly="True" SortExpression="fornecedor" />
                <asp:BoundField DataField="N_EFF" HeaderText="N_EFF" ReadOnly="True" SortExpression="N_EFF" >
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:BoundField>
                <asp:BoundField DataField="posição" HeaderText="posição" ReadOnly="True" SortExpression="posição" >
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:BoundField>
                <asp:BoundField DataField="Data" HeaderText="Data" ReadOnly="True" SortExpression="Data" Visible="False" />
                <asp:BoundField DataField="referencia" HeaderText="referencia" ReadOnly="True" SortExpression="referencia" />
                <asp:BoundField DataField="designação" HeaderText="designação" SortExpression="designação" ReadOnly="True" />
                <asp:BoundField DataField="qtt" HeaderText="qtt" SortExpression="qtt" ReadOnly="True" >
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:BoundField>
                <asp:BoundField DataField="qtt_recebida" HeaderText="qtt_recebida" SortExpression="qtt_recebida" >
                <ItemStyle BackColor="#CCCCCC" HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:BoundField>
                <asp:BoundField DataField="data_prevista" HeaderText="data_prevista" ReadOnly="True" SortExpression="data_prevista" Visible="False" />
                <asp:BoundField DataField="observações" HeaderText="observações" SortExpression="observações" />
                <asp:TemplateField HeaderText="dimenções" SortExpression="dimenções">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" >
                            <asp:ListItem Value="ok"></asp:ListItem>
                            <asp:ListItem Value="nok"></asp:ListItem>
                            <asp:ListItem Value="n/a"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("dimenções") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="certificado" SortExpression="certificado">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" >
                            <asp:ListItem Value="ok"></asp:ListItem>
                            <asp:ListItem Value="nok"></asp:ListItem>
                            <asp:ListItem Value="n/a"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("certificado") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="acabamento" SortExpression="acabamento">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" >
                            <asp:ListItem Value="ok"></asp:ListItem>
                            <asp:ListItem Value="nok"></asp:ListItem>
                            <asp:ListItem Value="n/a"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("acabamento") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CheckBoxField DataField="utilizado_em_produção" HeaderText="utilizado_em_produção" SortExpression="utilizado_em_produção" Visible="False" />
                <asp:BoundField DataField="obs" HeaderText="obs" SortExpression="obs" Visible="False" />
                <asp:CommandField ShowEditButton="True" />
            </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>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PIConnectionString %>" SelectCommand="SELECT bi.bistamp,
       bi.obrano                           AS N_EFF,
       bi.nome                             AS fornecedor,
       bi.posic                            AS posição,
       bi.bifref                           AS obra,
       CONVERT (CHAR(10), bi.atedata, 104) AS 'Data',
       bi.ref                              AS referencia,
       bi.design                           AS designação,
       bi.qtt,
       bi.u_num1                           AS qtt_recebida,
       CASE
         WHEN dedata <= '19000101' THEN Getdate()
         ELSE dedata
       END                                 AS data_prevista,
       bi.lobs2                            AS observações,
       bi2.u_dime                          AS dimenções,
       bi2.u_cetmat                        AS certificado,
       bi2.u_acab                          AS acabamento,
       bi2.u_utilprod                      AS utilizado_em_produção,
       bi.lobs3                            AS obs
FROM   bi WITH (nolock)
       INNER JOIN bi2
               ON bi.bistamp = bi2.bi2stamp
WHERE  nome like '%'+@box+'%'
       AND ( bi.qtt <> 0 )
       AND ( bi.ndos = 2 )
       AND ( bi.qtt > bi.u_num1 )

ORDER  BY n_eff ,Len(bi.posic),
          posição" UpdateCommand="MaterialRecebido_web" UpdateCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox2" Name="box" PropertyName="Text"  />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="qtt_recebida" Type="Decimal" />
                <asp:Parameter Name="bistamp" Type="String" />
                <asp:Parameter Name="observações" Type="String" />
                <asp:Parameter Name="dimenções" Type="String"  />
                <asp:Parameter Name="certificado" Type="String"   />
                <asp:Parameter Name="acabamento"  Type="String"  />
            </UpdateParameters>
        </asp:SqlDataSource>

    </form>
    <hr />
</body>
</html>



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.IO;
namespace MaterialRecebido
{
    public partial class WebForm1 : System.Web.UI.Page
    {


    protected void Page_Load(object sender, EventArgs e)
        {
           

        }
    }
}


When update i saw this on profile

SQL
exec MaterialRecebido_web @qtt_recebida=1.0000,@observações=N' teste',@dimenções=NULL,@certificado=NULL,@acabamento=NULL,@bistamp=N'CB17072740626,342000010  '


What I have tried:

I tried to change Parameter to ControlParameter on these 3 fields but it did not work.
Posted
Updated 19-Oct-22 22:32pm
v2

1 solution

You don't bind the selected value of the lists to the database field, so it won't display the current value, and it won't save the selected value in the database.
ASP.NET
<asp:TemplateField HeaderText="certificado" SortExpression="certificado">
    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("certificado") %>'>
            <asp:ListItem Value="ok"></asp:ListItem>
            <asp:ListItem Value="nok"></asp:ListItem>
            <asp:ListItem Value="n/a"></asp:ListItem>
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("certificado") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
 
Share this answer
 
v2
Comments
BrunoJAlves 21-Oct-22 7:03am    
Thanks Richard, but when i set up i can't edit and this error is showing.

[ArgumentOutOfRangeException: 'DropDownList1' has an invalid SelectedValue because it doesn't exist in the list of items.
parameter name: value]

You know that I'm missing?
Richard Deeming 21-Oct-22 7:06am    
Yes - the value you're loading from your database doesn't match one of the three values you've added to the list. You'll need to examine the data in your database to find out what the values should be.
BrunoJAlves 21-Oct-22 7:30am    
Thanks, You are the Man ;)
Just need to add this <asp:listitem value="" <="" asp:listitem=""> to DropDownList and it works.

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



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