我是Stackoverflow的新手。
在我的工作中,我被分配了创建脚本/程序的任务,该脚本/程序将记录访问我所在地区15所学校中的每一所学校的人员和工作人员进入访问数据库,以便管理人员可以查看它。我决定用C#来实现这个程序。
我遇到的代码问题是,当表单加载时,它将通过一系列IF语句,根据计算机主机名的2个字符确定他们所在的学校,然后提示用户输入他们的名字和他们访问学校的原因。
当我调试我的代码时,我输入我的姓名和原因然后我点击保存,它将输入人员姓名,人员访问原因,时间戳以及他们所在学校等信息到访问数据库中。但是当我点击保存并通过调试查看执行过程时,它只保存名称和原因,但不保存时间戳和学校。只有这样我才能保存时间戳和学校,如果我尝试编辑那些字符串值(不允许我编辑它,这很好),这是我不应该做的事情。
以下是我的代码,用户输入信息的表格:
namespace district_logprogram
{
public partial class district_CheckIn : Form
{
public district_CheckIn()
{
InitializeComponent();
txtTime.Text = DateTime.Now.ToString();
txtLocation.Text = ToString();
}
private void txtTime_TextChanged(object sender, EventArgs e)
{
//displays date and time on txtTime text box in Check In menu.
//On pressing 'Save' it will input date/time into checkIn column in district_checkIn table
var today = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");
txtTime.Text = today;
}
private void txtLocation_TextChanged(object sender, EventArgs e)
{
//displays which school you are located in
//On pressing 'Save' it will input the school into location column in district_checkIn table
System.Threading.Thread.Sleep(5);
if (System.Environment.MachineName.Contains("01"))
{
txtLocation.Text = "School 1";
}
else if (System.Environment.MachineName.Contains("02"))
{
txtLocation.Text = "School 02";
}
else if (System.Environment.MachineName.Contains("03"))
{
txtLocation.Text = "School 03";
}
else if (System.Environment.MachineName.Contains("04"))
{
txtLocation.Text = "School 04";
}
else if (System.Environment.MachineName.Contains("05"))
{
txtLocation.Text = "School 05";
}
else if (SystS6.Environment.MachineName.Contains("06"))
{
txtLocation.Text = "School 06";
}
else if (System.Environment.MachineName.Contains("07"))
{
txtLocation.Text = "School 07";
}
else if (System.Environment.MachineName.Contains("08"))
{
txtLocation.Text = "School 08";
}
else if (System.Environment.MachineName.Contains("09"))
{
txtLocation.Text = "School 09";
}
else if (System.Environment.MachineName.Contains("10"))
{
txtLocation.Text = "School 10";
}
else if (System.Environment.MachineName.Contains("11"))
{
txtLocation.Text = "School 11";
}
else if (System.Environment.MachineName.Contains("12"))
{
txtLocation.Text = "School 12";
}
else if (System.Environment.MachineName.Contains("13"))
{
txtLocation.Text = "School 13";
}
else if (System.Environment.MachineName.Contains("14"))
{
txtLocation.Text = "School 14";
}
else if (System.Environment.MachineName.Contains("15"))
{
txtLocation.Text = "School 15";
}
else if (System.Environment.MachineName.Contains("16"))
{
txtLocation.Text = "School 16";
}
else if (System.Environment.MachineName.Contains("17"))
{
txtLocation.Text = "School 17";
}
else if (System.Environment.MachineName.Contains("18"))
{
txtLocation.Text = "School 18";
}
else if (System.Environment.MachineName.Contains("19"))
{
txtLocation.Text = "School 19";
}
else if (System.Environment.MachineName.Contains("20"))
{
txtLocation.Text = "School 20";
}
else if (System.Environment.MachineName.Contains("21"))
{
txtLocation.Text = "School 21";
}
else if (System.Environment.MachineName.Contains("22"))
{
txtLocation.Text = "School 22";
}
else if (System.Environment.MachineName.Contains("23"))
{
txtLocation.Text = "School 23";
}
else
{
//If computer hostname is not configured correctly, it will display message below
btnSave.Enabled = false;
txtLocation.Text = "Cannot determine school name! Check hostname!";
}
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
districtcheckinBindingSource.EndEdit();
district_checkinTableAdapter.Update(this.districtDB.district_checkin);
Close(); //this closes the check in menu
MessageBox.Show("You are checked in!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "An error occurred during check in!", MessageBoxButtons.OK, MessageBoxIcon.Error);
districtcheckinBindingSource.ResetBindings(false);
}
}
private void district_CheckIn_Load(object sender, EventArgs e)
{
this.district_checkinTableAdapter.Fill(this.districtDB.district_checkin); //opens connection to district_checkin table
districtcheckinBindingSource.DataSource = this.districtDB.district_checkin; //opens connection to district_checkin table
txtName.Focus(); //focuses cursor on txtName text box field
this.districtDB.district_checkin.Adddistrict_checkinRow(this.districtDB.district_checkin.Newdistrict_checkinRow()); //begins a new row for adding records
districtcheckinBindingSource.MoveLast(); //moves new record to end of table - "shifts it down"
}
}
}
以下是输入信息后调试时的样子: only the name and reason are inputted but not time and school name 以下是输入信息后的样子: what the input should look like before being inserted 有关如何解决此问题的任何建议都会有很大帮助! :)
答案 0 :(得分:0)
比较strings
时,您应该确保比较案例,即大写和小写。
因此要与UpperCase
或LowerCase
if (System.Environment.MachineName.ToUpperInvariant().Contains("SS"))
同样适用于其他条件。