gpt4 book ai didi

c# - 将标签中的值与文本框中输入的值进行比较,以从 MySQL 数据库中获取值 c# asp.net

转载 作者:行者123 更新时间:2023-11-29 21:43:31 25 4
gpt4 key购买 nike

我在 stackoverflow 和 google 上到处查看,但没有找到任何可以使用的成功答案,所以我决定在这里发布我的问题。

我有一个asp标签,显示用户可以分配的优惠券总数,还有一个asp文本框,要求用户输入他们想要分配的优惠券数量。

我需要找到一种方法来检查文本框中输入的值是否小于或等于标签中的数字。
我在我的 ascx 页面上使用比较验证器,但这似乎不起作用。

这是我目前拥有的代码

这是我的前端代码:ascx

    <h4>
<b>
<asp:Label runat="server" ID="lblAvailableVoucherCount" Visible="false" ></asp:Label>
<asp:Label runat="server" ID="lblCount" Visible="false"></asp:Label>
</b>
</h4>
<table>
<tr>
<td>
<b>
<asp:Label runat="server" ID="lblGrp" Text="CHOOSE A GROUP" Visible="false"></asp:Label>
</b>
<asp:DropDownList runat="server" ID="GroupsDropDownList" Width="160px"
AutoPostBack="false" Visible ="false"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<b>
<asp:Label runat="server" ID="number"
Text="ENTER THE NUMBER OF VOUCHERS YOU WANT TO ASSIGN TO THE GROUP"
Visible="false" ></asp:Label>
</b>
<asp:TextBox ID="noOfVouchers" runat="server" Visible = "false"/>
</td>
<td>
<asp:CompareValidator runat="server"
ErrorMessage="You DO NOT have enough vouchers available to be assigned to the group!"
ControlToValidate="noOfVouchers" ValueToCompare="lblCount"
Operator="LessThanEqual" Type="String" ForeColor="#FF0000"
Font-Bold="true"></asp:CompareValidator>
</td>
<td>
<asp:RequiredFieldValidator runat="server"
ID="RequiredFieldValidatorNumVouchers"
ControlToValidate="noOfVouchers" Text="Required"
ErrorMessage="You MUST enter the number of vouchers you wish to assign the the group!"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lbltest2" ForeColor="#FF0000"
Font-Bold="true" Visible="true"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="assign" Text="Assign"
OnClick="assign_voucher_click" Visible="false" Width="150"
Height="35px"></asp:Button>
</td>
</tr>
</table>

这是我的后端代码:ascx.cs

//Assigning Vouchers to Groups
protected void assign_voucher_click(object sender, System.EventArgs e)
{
group_name = GroupsDropDownList.SelectedValue;
string InstId = "";

using (OdbcConnection dbConnection = new OdbcConnection(ConnStr))
{
dbConnection.Open();
{
OdbcCommand dbCommand = new OdbcCommand();
dbCommand.Connection = dbConnection;

dbCommand.CommandText = @"SELECT voucher_no_pk FROM vouchers WHERE admin_admin_email_id = ? AND voucher_status = 'NEW' LIMIT " + Convert.ToInt32(noOfVouchers.Text) + ";";
dbCommand.Parameters.AddWithValue("@admin_admin_email_id", username);
dbCommand.Parameters.AddWithValue("LIMIT", noOfVouchers.Text.ToString());
dbCommand.ExecuteNonQuery();

OdbcDataReader dataReader = dbCommand.ExecuteReader();

while (dataReader.Read())
{
Voucher_list.Add(dataReader["voucher_no_pk"]);
}

if (Voucher_list.Count == 0)
{
lblCount.Text = "Sorry your DO NOT have enough vouchers available";
}

dataReader.Close();

OdbcCommand dbCommand1 = new OdbcCommand();
dbCommand1.Connection = dbConnection;

//fetching the group id for the selected group name
dbCommand1.CommandText = @"SELECT group_id_pk FROM vouchergroup WHERE group_name = ? AND admin_admin_email_id = ?;";
dbCommand1.Parameters.AddWithValue("@group_name", group_name);
dbCommand1.Parameters.AddWithValue("@admin_admin_email_id", username);
dbCommand1.ExecuteNonQuery();

OdbcDataReader dataReader1 = dbCommand1.ExecuteReader();
while (dataReader1.Read())
{
group_id = dataReader1["group_id_pk"].ToString();
}

dataReader1.Close();

lbltest2.Text = "group id selected for group:'" + group_name + "'";

OdbcCommand dbCommand4 = new OdbcCommand();
dbCommand4.Connection = dbConnection;
dbCommand4.CommandText = @"SELECT instructor_inst_email FROM vouchergroup WHERE group_id_pk = " + group_id + ";";
dbCommand4.ExecuteNonQuery();

OdbcDataReader dataReader4 = dbCommand4.ExecuteReader();
while (dataReader4.Read())
{
InstId = dataReader4["instructor_inst_email"].ToString();
}

dataReader4.Close();

OdbcCommand dbCommand2 = new OdbcCommand();
dbCommand2.Connection = dbConnection;

//updating the vouchers table with the group id, voucher status and instructor email id
for (int i = 0; i < (Voucher_list.Count); i++)
{
dbCommand2.CommandText = @"UPDATE vouchers SET voucher_group_voucher_group_id =" + group_id + ", voucher_status = 'Available to Assign', instructor_instructor_email = ? WHERE voucher_no_pk = " + Convert.ToInt32(Voucher_list[i]) + ";";
dbCommand2.Parameters.AddWithValue("@instructor_instructor_email", InstId);
dbCommand2.ExecuteNonQuery();
}

createInstructorEmail(InstId, group_name, Voucher_list.Count);

if (Voucher_list.Count != 0)
{
lbltest2.Text = "Congratulations! Your vouchers have been assigned to group:<i>" + group_name + "</i>";
}

OdbcCommand dbCommand3 = new OdbcCommand();
dbCommand3.Connection = dbConnection;

dbCommand3.CommandText = @"SELECT vg.group_name AS 'GROUP NAME', vg.instructor_inst_email AS 'GROUP OWNER EMAIL', count(vo.voucher_no_pk) AS `count` FROM vouchergroup vg LEFT JOIN vouchers vo ON vg.group_id_pk = vo.voucher_group_voucher_group_id AND vo.admin_admin_email_id = ? GROUP BY vg.group_id_pk ;";
dbCommand3.Parameters.AddWithValue("@admin_admin_email_id", username);
dbCommand3.ExecuteNonQuery();

OdbcDataReader dataReader2 = dbCommand3.ExecuteReader();
if (dataReader2.HasRows)
{
groupList.DataSource = dataReader2;
groupList.DataBind();

dataReader2.Close();
}
else
{
assign.Visible = false;
lblGrp.Visible = false;
GroupsDropDownList.Visible = false;
number.Visible = false;
noOfVouchers.Visible = false;
lblgroupcreated.Text = "You DO NOT have any Groups yet. Create a New Group and then assign vouchers to them!";
}

}
dbConnection.Close();
}

noOfVouchers.Text = "";
assignMore.Visible = true;
assign.Visible = false;
}

我一直在尝试我能想到的一切,所以任何帮助将不胜感激。
我有一种感觉,我在 ascx.cs 代码中的某处缺少 if else 语句,这可能是原因,但由于我对 c# 和 asp.net 相当陌生,所以我不确定。

最佳答案

如果您要比较两个控件,那就是:

controltocompare 而不是 ValueToCompare

controltovalidate="noOfVouchers"
controltocompare="lblCount"

示例:

<asp:TextBox runat="server" id="txtSmallNumber" />
<asp:TextBox runat="server" id="txtBigNumber" />

<asp:CompareValidator runat="server" id="cmpNumbers" controltovalidate="txtSmallNumber" controltocompare="txtBigNumber" operator="LessThan" type="Integer" errormessage="The first number should be smaller than the second number!" /><br />

关于c# - 将标签中的值与文本框中输入的值进行比较,以从 MySQL 数据库中获取值 c# asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34277092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com