私はStackoverflowを初めて使用しています。C#プログラムIf文発行
私の仕事では、私の地区にある15以上の学校を訪れる人やスタッフをアクセスデータベースに記録するスクリプト/プログラムを作成するタスクが割り当てられており、管理者がそれを見直すことができます。私はこのプログラムを実現させるためにC#を使うことにしました。
私のコードで実行している問題は、フォームが読み込まれると、ユーザーに入力を促す前に、コンピューターのホスト名の2文字に基づいて、学校の種類を判断する一連のIFステートメントを実行することです彼らの名前と彼らが学校を訪問している理由。
自分のコードをデバッグするときに、自分の名前と理由を入力した後、[保存]をクリックすると、人の名前、人の訪問理由、タイムスタンプ、学校の所在地などの情報をアクセスデータベースに入力します。しかし、保存をクリックしてデバッグを介して実行プロセスを見ると、名前と理由は保存されますが、タイムスタンプと学校は保存されません。私がタイムスタンプと学校を保存するために得ることができる唯一の方法は、私がそれらの文字列の値を編集しようとしている場合(それは私がそれを編集することはできません。ここで
は、ユーザが情報を入力するフォームの下に私のコードです:ここでは
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 この問題を解決する方法についてのご意見は、大いに役立ちます。 :)
23(!)の同じコードスタブを1つのものに置き換えるループを作成してください。 – Gustav
私はこれを私のコードで行いました。提案に感謝します! – mrdrumboy21
それはあまりできません。メンテナンスと可読性については、より多くの質問です。 – Gustav