简体   繁体   中英

Visual Studio 2010 Ignoring All query Errors?

I am using a Visual Studio 2010 and ASP.net c# language. I am try to read from excel sheet then insert the information to the Microsoft SQL database, but I found a problem it ignoring the error in the query statement.

for (int i = 0; i < objdatasetdto.Tables[0].Rows.Count; i++)
{
  start_time = Convert.ToDateTime(objdatasetdto.Tables[0].Rows[i]["start"].ToString());
  end_time = Convert.ToDateTime(objdatasetdto.Tables[0].Rows[i]["end"].ToString());
  if (objdatasetdto.Tables[0].Rows[i]["Lecture_day"].ToString().Equals("1"))
  {
     try
     {
        query = "Insert into [M].[Lecture]([dd],[start_time],[end_time],[week_no],[sec_no],
                 [room_no],[building_no]) "+
                 values('" + Calendar1.SelectedDate.ToShortDateString() + "','" +
                 start_time.ToShortTimeString() + "','" + end_time.ToShortTimeString() + 
                 "','1','" + objdatasetdto.Tables[0].Rows[i]["section_no"].ToString() + "','" 
                 + objdatasetdto.Tables[0].Rows[i]["room_no"].ToString() + "','" 
                 + objdatasetdto.Tables[0].Rows[i]["building_no"].ToString() + "');";
        ifexist = new SqlCommand(query, cnn);
      }
      catch (Exception ex)
      {
         Response.Write(ex);
      }
    }//end if
 }// end for loop

I wrote [dd] column instead of [date] to test if it detect the error or not. but it just ignores them completely.

How can I solve this problem.

You don't appear to be executing the SqlCommand. Try adding:

ifexist.ExecuteScalar();

where you have executed the query?
You have not written anything in one of the following

  1. ifexist. ExecuteScalar() ;
  2. ifexist. ExecuteNonQuery() ;

If you don't execute you query how the query will be compiled and how will you expect an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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