Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过验证将GridView数据发送到数据库_C#_Asp.net_Validation - Fatal编程技术网

C# 通过验证将GridView数据发送到数据库

C# 通过验证将GridView数据发送到数据库,c#,asp.net,validation,C#,Asp.net,Validation,我是asp.net开发者的创始人, 网格视图包含 ProductID, ProductName, Price, Qty, Total 默认设置为五列 如果选择产品名称,则价格将自动显示,但数量将由用户输入。 如果未输入数量,则显示消息, 如果任何一列填写完毕,是否保存数据库 productName是dropdownlist,我需要服务器端代码 在我的代码中 受保护的无效btnSave\u单击(对象发送方,事件参数e) { SqlDataAdapter-sda; SqlCommand命令cmd;

我是asp.net开发者的创始人, 网格视图包含

ProductID, ProductName, Price, Qty, Total
默认设置为五列

如果选择产品名称,则价格将自动显示,但数量将由用户输入。 如果未输入数量,则显示消息, 如果任何一列填写完毕,是否保存数据库

productName是dropdownlist,我需要服务器端代码 在我的代码中

受保护的无效btnSave\u单击(对象发送方,事件参数e) {

SqlDataAdapter-sda;
SqlCommand命令cmd;
DateTime savedate=DateTime.ParseExact(txtBillDate.Text.Trim()+“”+DateTime.Now.ToString(“hh:mm:ss tt”),“dd/mm/yyyy hh:mm:ss tt”,null);
文本框txtProductID、txtPrice、txtQty、txtTotal;
DropDownList ddlProductName;
DataTable mdt=新DataTable();
标签lblGrandTotal;
if(DataCheck())
{
如果(txtMobileNumber.Text!=“”)
{
con.Open();
cmd=new SqlCommand(“插入计费(BillNumber、BillDate、CustomerName、CustomerMobile)值(“+txtBillNumber.Text+”、“+savedate+”、“+DDLCusterName.SelectedItem.Text+”、“+txtMobileNumber.Text+”)、con);
对于(int i=0;i
您可以将asp.net必需的字段验证程序添加到gridview中的每个文本框中

<asp:TextBox id="txtQty" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="reqQty" ControlToValidate="txtQty" ErrorMessage="*"></asp:RequiredFieldValidator>
ASPX


C#按钮单击处理程序以获取用户输入的数量

protected void cmdTest_Click(object sender, EventArgs e)
{
    for (int i = 0; i < gvTest.Rows.Count; i++)
    {
        int qty = Convert.ToInt32(((TextBox)gvTest.Rows[i].FindControl("txtQty")).Text);
        // code here
    }
}
protectedvoid cmdTest\u单击(对象发送方,事件参数e)
{
对于(int i=0;i
验证结果


如果您在所有五列中都使用默认设置,那么您将无法使用requiredfieldvalidator。请尝试使用regularexpressionvalidator。

requiredfieldvalidator或所有txtQty,但取决于select ProductName。我需要c#中的服务器端代码。如果您应用requiredfieldvalidator,则无论产品名称或名称如何,它都将应用其他人也尝试过吗?如果你有一个gridview,其中的行(itemtemplates)中有输入字段,并为它们分配了验证器,那么每个元素都会分别进行验证,因为每一行都会生成一个元素和验证器。我已经修改了我的答案,以进一步说明我的示例。我希望我没有误解这个问题。
create table products
( 
    id int identity(1,1),
    name varchar(500),
    price decimal(18,2)
)

insert into products values ('Soap', 15.0)
insert into products values ('Provitas', 25.0)
insert into products values ('Paper', 10.0)
insert into products values ('Foam Bath', 150.0)
    <asp:GridView ID="gvTest" runat="server" DataSourceID="SqlTest" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="ID" DataField="ID" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
            <asp:BoundField HeaderText="Price" DataField="Price" />
            <asp:TemplateField HeaderText="Qty">
                <ItemTemplate>
                    <asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqQty" runat="server" ControlToValidate="txtQty" ErrorMessage="*"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlTest" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=play;Persist Security Info=True;User ID=user;Password=userpassword" SelectCommand="SELECT id, name, price FROM Products"></asp:SqlDataSource>
    <asp:Button ID="cmdTest" runat="server" Text="Submit" />
protected void cmdTest_Click(object sender, EventArgs e)
{
    for (int i = 0; i < gvTest.Rows.Count; i++)
    {
        int qty = Convert.ToInt32(((TextBox)gvTest.Rows[i].FindControl("txtQty")).Text);
        // code here
    }
}