简体   繁体   English

asp.net c#检查错误

[英]asp.net c# checking error

I want to check a selected date out of txtFrom and compare if that specific date exists. 我想检查txtFrom的选定日期,并比较该特定日期是否存在。 It should give me an alert, but it doesn't check. 它应该给我一个警报,但它不会检查。 As a beginner I don't know if I wrote the code well, please can somebody help me? 作为初学者,我不知道我是否编写了良好的代码,请有人帮助我吗? These are my codes: 这些是我的代码:

string conn = WebConfigurationManager.ConnectionStrings["DIVIHOTELConnectionString"].ConnectionString;
SqlConnection myconn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from MyBooking where FromDate='" + txtFrom.Text + "'", myconn);
myconn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (txtFrom.Text == dr.GetString(1))
{
    ScriptManager.RegisterStartupScript(this, GetType(), "showalert","alert('This particular date has been booked already, please select new date !');", true);
}

It is very clear that if your query returns column in this sequence [ID] ,[ClientID] ,[RoomID] ,[Amount] ,[DateBooked] ,[IsInBook] ,[FromDate] ,[EndDate] and you accessing dr.getString(1) then this will return value for [ClientID] not for [FromDate] . 很明显,如果您的查询返回此序列中的列[ID] ,[ClientID] ,[RoomID] ,[Amount] ,[DateBooked] ,[IsInBook] ,[FromDate] ,[EndDate]并且您访问dr.getString(1)然后这将返回[ClientID]值而不是[FromDate]

Try to use exact sequence number of column so that to get work done. 尝试使用列的确切序列号,以便完成工作。

if (txtFrom.Text == dr.GetString(7))
{                          ------^
     //your code here
}

My Suggestion 我的建议

Check what you storing in db and what is your input date which you are comparing both are same or not, because your datatype is varchar 检查您在db中存储的内容以及您要比较的输入日期是否相同,因为您的数据类型是varchar

As per my understating you will get result when your time also match with date. 根据我的不足,当你的时间也与日期相符时,你会得到结果。 likely i will say you will not get result with your current scenario. 我可能会说你不会得到你当前场景的结果。

首先,使用适当的日期格式过滤查询,即参数为数据格式其次,在从datareader读取数据时,指定正确的列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM