C# 不确定此单选按钮为何抛出错误?
我目前正在做一个健身班预订系统,我正在做一些研究,请与我裸 我已经完成了大部分代码,但是我的第二个单选按钮在选择你想要的类时遇到了一个奇怪的问题 我已经设置了我的代码,因此如果您输入的会员ID已注册到您选择的健身类,则会出现一个消息框。对于我的RadioButton1(rbCardioClass)和RadioButton2(rbPilatesClass),错误消息框工作得很好,并且工作正常。但是我的RadioButton2(rbSpinClass)会使错误消息框每次都出现,即使MemberID与“SpinClass”没有关联 我尝试过使用不同的if语句、不同的单选按钮等,但似乎无法让它按我想要的方式工作 如果我转到我的C# 不确定此单选按钮为何抛出错误?,c#,database,radio-button,dataset,messagebox,C#,Database,Radio Button,Dataset,Messagebox,我目前正在做一个健身班预订系统,我正在做一些研究,请与我裸 我已经完成了大部分代码,但是我的第二个单选按钮在选择你想要的类时遇到了一个奇怪的问题 我已经设置了我的代码,因此如果您输入的会员ID已注册到您选择的健身类,则会出现一个消息框。对于我的RadioButton1(rbCardioClass)和RadioButton2(rbPilatesClass),错误消息框工作得很好,并且工作正常。但是我的RadioButton2(rbSpinClass)会使错误消息框每次都出现,即使MemberID与
servicesErrorCheck(string[]description)
方法,只将temp
变量设置为true
所有单选按钮都正确地保存到数据库表中,但我会丢失我的错误,这使我认为这可能与我设置消息框的方式有关
以下是原型表单的屏幕截图,仅供参考
下面是应用程序运行时表格的屏幕截图
以下是使用MemberID引发的错误,MemberID没有与之关联的“Spin”类
这是我的问题代码-
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)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.SqlClient;
使用系统配置;
命名空间成员资格\u Formv2
{
公共部分舱位舱位订票:表格
{
公共设施类别预订()
{
初始化组件();
}
private void b主菜单\单击(对象发送者,事件参数e)
{
新建主菜单().Show();
this.Hide();
}
私有void fitnessInformationBindingNavigatorSaveItem_单击(对象发送者,事件参数e)
{
这个。Validate();
此.fitnessInformationBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.databaseDataSet);
}
私有void FitnessClassBooking_Load(对象发送方,事件参数e)
{
//TODO:这行代码将数据加载到“databaseDataSet.Members”表中。您可以根据需要移动或删除它。
this.membersTableAdapter.Fill(this.databaseDataSet.Members);
//TODO:这行代码将数据加载到“databaseDataSet.FitnessInformation”表中。您可以根据需要移动或删除它。
this.fitnessInformationTableAdapter.Fill(this.databaseDataSet.FitnessInformation);
}
私有void fitnessInformationDataGridView_CellContentClick(对象发送者,DataGridViewCellEventArgs e)
{
}
私有字符串descriptionfit()
{
字符串className=“”;
if(rbCardioClass.Checked==true)
{
className=“有氧运动”;
}
else if(rbSpinClass.Checked==true)
{
className=“自旋”;
}
else if(rbPilatesClass.Checked==true)
{
className=“普拉提”;
}
返回类名;
}
私有字符串classDescription()
{
字符串serviceSeletionString=“Class”;
if(rbCardioClass.Checked==true)
{
serviceSeletionString=rbCardioClass.Text;
这个。刷新();
}
else if(rbSpinClass.Checked==true)
{
serviceSeletionString=rbSpinClass.Text;
这个。刷新();
}
else if(rbPilatesClass.Checked==true)
{
serviceSeletionString=rbPilatesClass.Text;
这个。刷新();
}
返回serviceSeletionString;
}
私有布尔错误检查ID()
{
bool statusDB=true;
//从MembersTa表获取行信息
DatabaseDataSet.MembersRow newEntry=DatabaseDataSet.Members.FindByMemberID(Int32.Parse(textBox3.Text));
//从预订表中获取信息
if(newEntry==null)
{
statusDB=false;
返回(statusDB);
}
返回(statusDB);
}
公共布尔服务错误检查(字符串[]说明)
{
布尔温度=真;
字符串serviceSeletionString=“”;
if(rbCardioClass.Checked==true)
{
serviceSeletionString=rbCardioClass.Text;
这个。刷新();
}
else if(rbSpinClass.Checked==true)
{
serviceSeletionString=rbSpinClass.Text;
这个。刷新();
}
else if(rbPilatesClass.Checked==true)
{
serviceSeletionString=rbPilatesClass.Text;
这个。刷新();
}
for(int t=0;tserviceSeletionString.Contains(description[t].Trim())