【问题标题】:Not sure why this Radio Button is throwing an error?不知道为什么这个单选按钮会引发错误?
【发布时间】:2021-05-17 20:55:54
【问题描述】:

我目前正在为我正在做的一些研究制作健身课程预订系统,请与我联系。

我已经完成了大部分代码,但是我的第二个单选按钮出现了这个奇怪的问题,用于选择你想要的类。

我已设置我的代码,如果您输入的会员 ID 已注册到您选择的健身课程,则会出现一个消息框。对于我的 RadioButton1 (rbCardioClass) 和 RadioButton2 (rbPilatesClass),错误消息框效果很好,并且可以正常工作。但是我的 RadioButton2 (rbSpinClass) 每次都会出现错误消息框,即使 MemberID 没有与“Spin Class”相关联。

我尝试了 if 语句的不同用法、不同的单选按钮等,但似乎无法让它按我想要的方式工作。

如果我去我的servicesErrorCheck(string[] description)method 并且只是temp 变量到true 所有单选按钮都正确保存到数据库表但是我失去了我的错误,这让我认为这与可能是我设置消息框的方式。

这里是原型表单的截图,仅供参考。 FitnessClassBooking Form

这是应用程序运行时表格的屏幕截图App Running Fitness Form

这是使用没有关联“Spin”类的 MemberID 引发的错误App Running Error

这是我有问题的代码 -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Membership_Formv2
{
public partial class FitnessClassBooking : Form
{
    public FitnessClassBooking()
    {
        InitializeComponent();
    }

    private void bMainMenu_Click(object sender, EventArgs e)
    {
        new MainMenu().Show();
        this.Hide();
    }

    private void fitnessInformationBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.fitnessInformationBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.databaseDataSet);

    }

    private void FitnessClassBooking_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'databaseDataSet.Members' table. You can move, or remove it, as needed.
        this.membersTableAdapter.Fill(this.databaseDataSet.Members);
        // TODO: This line of code loads data into the 'databaseDataSet.FitnessInformation' table. You can move, or remove it, as needed.
        this.fitnessInformationTableAdapter.Fill(this.databaseDataSet.FitnessInformation);



    }

    private void fitnessInformationDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private string descriptionfit()
    {

        string className = "";

       

        if (rbCardioClass.Checked == true)
        {
            className = "Cardio";
        }
        else if (rbSpinClass.Checked == true)
        {
            className = "Spin";
        }
        else if (rbPilatesClass.Checked == true)
        {
            className = "Pilates";
        }

        return className;

    }

    private string classDescription()
    {
        string serviceSeletionString = "Class";

        if (rbCardioClass.Checked == true)
        {
            serviceSeletionString = rbCardioClass.Text;
            this.Refresh();
        }
        else if (rbSpinClass.Checked == true)
        {
            serviceSeletionString = rbSpinClass.Text;
            this.Refresh();
        }
        else if (rbPilatesClass.Checked == true)
        {
            serviceSeletionString = rbPilatesClass.Text;
            this.Refresh();
        }

        return serviceSeletionString;
    }

    private bool errorCheckingID()
    {
        bool statusDB = true;
        //Getting row info from MembersTa table
        DatabaseDataSet.MembersRow newEntry = databaseDataSet.Members.FindByMemberID(Int32.Parse(textBox3.Text));

        //Getting information from BookingTa table
        if (newEntry == null)
        {
            statusDB = false;
            return (statusDB);
        }
        return (statusDB);
    }


    public bool servicesErrorCheck(string[] description)
    {
        bool temp = true;
        string serviceSeletionString = "";

        if (rbCardioClass.Checked == true)
        {
            serviceSeletionString = rbCardioClass.Text;
            this.Refresh();
        }
        else if (rbSpinClass.Checked == true)
        {
            serviceSeletionString = rbSpinClass.Text;
            this.Refresh();
        }
        else if (rbPilatesClass.Checked == true)
        {
            serviceSeletionString = rbPilatesClass.Text;
            this.Refresh();
        }


        for (int t = 0; t < description.Length; t++)
        {
            if (serviceSeletionString.Contains(description[t].Trim()))
            {
                temp = false;
                break;
            }
        }
        return (temp);
    }

    private string originalaccesdb()
    {
        string a = "";

        DatabaseDataSet.FitnessInformationRow newRow = databaseDataSet.FitnessInformation.NewFitnessInformationRow();

        newRow.Fitness_Booking_ID = databaseDataSet.FitnessInformation.Count + 1;
        newRow.Description = descriptionfit();
        newRow.MemberID = Int32.Parse(textBox3.Text);
        databaseDataSet.FitnessInformation.AddFitnessInformationRow(newRow);

        return a;
    }

    private string[] accessDB()
    {
        int t = 0;
        int temp;
        string[] servicesList = { "n", "n", "n" };  //This variable will store the data

        //Same code too extract table information
        foreach (DataRow r in databaseDataSet.FitnessInformation.Rows)
        {
            temp = Int32.Parse(r["MemberID"].ToString());

            if (temp == Int32.Parse(textBox3.Text))
            {
                //Store inside the array all the services/description against the ID.
                //Note that this array will remain "" for all the elements inside the array
                //if no descritopn/services (i.e., record) is found against the input ID
                servicesList[t] = r["Description"].ToString();
                t = t + 1;
            }
        }
        return (servicesList);
    }


    private void button1_Click(object sender, EventArgs e)
    {

        

        string text = textBox1.Text;
        textBox1.Text = "";
        int a = Int32.Parse(textBox2.Text);
        DatabaseDataSet.MembersRow newID = databaseDataSet.Members.FindByMemberID(Int32.Parse(textBox2.Text));

        string booking = "";
        int temp;

        foreach (DataRow r in databaseDataSet.FitnessInformation.Rows)
        {
            temp = Int32.Parse(r["MemberID"].ToString());

            if (temp == Int32.Parse(textBox2.Text))
            {
                booking = r["Description"].ToString() + ", " + booking;
            }
        }

        textBox1.Text = "Member ID is: " + (newID.MemberID).ToString() + Environment.NewLine +
                        "First Name is: " + (newID.First_Name).ToString() + Environment.NewLine +
                        "Last Name is: " + (newID.Last_Name).ToString() + Environment.NewLine +
                        "Bookings: " + booking;

        
    }

    public void button2_Click(object sender, EventArgs e)
    {
        

        bool status1, status2;
        string[] description;

        //Error control at the outer level for valid ID
        status1 = errorCheckingID();

        //Proceed only if ID is valid or status1 is true
        if (status1)
        {
            //Retrieve information from the other database. Ideally you want this method to return
            //an array containing registered services. This would be an array of strings.
            description = accessDB();

            //Services error checking
            status2 = servicesErrorCheck(description);

            //Now this is the code that would call the method to save data ito database
            //when status2 and 2 are true
            if (status2)
            {

                //Code for saving into database. 
                DatabaseDataSet.FitnessInformationRow newRow = databaseDataSet.FitnessInformation.NewFitnessInformationRow();

                newRow.Fitness_Booking_ID = databaseDataSet.FitnessInformation.Count + 1;
                newRow.Description = classDescription();
                newRow.MemberID = Int32.Parse(textBox3.Text);
                databaseDataSet.FitnessInformation.AddFitnessInformationRow(newRow);
            }
            else
            {
                //Show error that this service is not available
                MessageBox.Show("This Class is already assigned to that Member ID");
            }
        }
        else
        {
            //Error message invalid ID
            MessageBox.Show("Invalid ID");
        }
    }

    private void radioButton1_Click(object sender, EventArgs e)
    {

    }

    private void radioButton4_Click(object sender, EventArgs e)
    {

    }

    private void radioButton3_Click(object sender, EventArgs e)
    {

        }
    }
}

我真的不知道为什么会这样,所以我非常感谢任何帮助。

【问题讨论】:

    标签: c# database radio-button dataset messagebox


    【解决方案1】:

    您正在检查descriptions 中的每个字符串,如下所示:

    serviceSeletionString.Contains(description[t].Trim())
    

    考虑到至少一个字符串只是n,它将始终匹配Spin
    我不知道为什么你总是返回一个带有n 的空白数组的原因。我个人只会使用List&lt;string&gt;Add 从数据库到它的每个项目。但这是一个单独的点。

    要么将其更改为serviceSeletionString == description[t](不明白为什么需要Trim()),要么将整个foreach 循环替换为description.Contains(serviceSeletionString)

    【讨论】:

    • 您好。感谢您的建议,非常感谢。我尝试添加您在上面建议的代码,尽管他们让我将新记录添加到数据库中,但如果“memberID”已经与“类”相关联,它就不会再出错了......有什么想法吗?
    • 您好像错过了databaseDataSet.SaveChanges() 可能吗?您需要逐步调试此过程,并在所有点上验证变量是否符合您的预期。我无法运行您的代码,我只是注意到弹出的内容。
    • 好的,谢谢@charlieface。我会继续寻找。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多