[英]Compare array with TextBox to check for repeated values in database
I had no idea of how to call out the method to check if the database has the same Station name if the user was creating it. 如果用户创建数据库,我不知道如何调出方法来检查数据库是否具有相同的工作站名称。 I managed to store the column of the database to array but I still can't get a way to check. 我设法将数据库的列存储到数组中,但是我仍然找不到检查的方法。 Can anybody help me with it? 有人可以帮我吗? I kinda stuck now. 我有点卡住了。
This is the code in the button handler that generate the creating of data into database. 这是按钮处理程序中的代码,用于将数据创建到数据库中。
private void btnCreate_Click(object sender, EventArgs e)
{
using (testEntities Setupctx = new testEntities())
{
station creStation = new station();
creStation.Station1 = txtStation.Text;
creStation.Seats = cbSeats.SelectedItem.ToString();
Setupctx.stations.AddObject(creStation);
Setupctx.SaveChanges();
txtStation.Text = "";
cbSeats.SelectedIndex = -1;
MessageBox.Show("New Station Has Been Created.");
}
}
This is the code that I store the column into the Array. 这是我将列存储到数组中的代码。
private string[] StationNameList()
{
using (testEntities Setupctx = new testEntities())
{
return Setupctx.stations.Select(x => x.Station1).OrderBy(x => x).ToArray();
}
}
Can anyone help? 有人可以帮忙吗? Your help will be greatly appreciated. 对你的帮助表示感谢。
If you want to check if the station already exist in the data base then in btnCreate_Click
before adding the station you may get a list of stations from your method StationNameList
and then check if the station name entered in the textbox already exists in the list. 如果要检查数据库中是否已存在该工作站,则在添加该工作站之前,请在btnCreate_Click
,您可以从StationNameList
方法中获得一个工作站列表,然后检查在文本框中输入的工作站名称是否已存在于列表中。 You can do 你可以做
private void btnCreate_Click(object sender, EventArgs e)
{
using (testEntities Setupctx = new testEntities())
{
string[] stations = StationNameList();
if(stations.Contains(txtStation.Text))
{
//already exists
}
else
{
//does not exists
//your code to insert the new object
}
}
}
Try this 尝试这个
private void btnCreate_Click(object sender, EventArgs e)
{
using (testEntities Setupctx = new testEntities())
{
station creStation = new station(){Station1=txtStation.Text,Name=NameTextbox.text,Seats = cbSeats.SelectedItem.ToString()};
if (!Setupctx.Stations.Any(st => st.Name == creStation.Name))
{
Setupctx.stations.AddObject(creStation);
Setupctx.SaveChanges();
txtStation.Text = "";
cbSeats.SelectedIndex = -1;
MessageBox.Show("New Station Has Been Created.");
}
}
}
I hope this will help , and i expect you has Name property in Station class and is bind to some Textbox in UI. 我希望这会有所帮助,并且我希望您在Station类中具有Name属性,并绑定到UI中的某些Textbox。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.