Rishi Chatterjee

Rishi Chatterjee

  • NA
  • 116
  • 4.8k

Unable to Read Excel data in Asp.net

Jun 25 2020 2:09 AM
I have a excel sheet1 and sheet2.I want to check excel data is exist or not exist in database.First loop working fine. Now the problem is in second loop data is exist or not exist same error showing("Data Already Exist"). not able to Read data.please give me Solution for that.
  1. private bool ValidateUserBO(DataTable sheet1, DataTable sheet2) {  
  2.  try {  
  3.   bool isValid = true;  
  4.   string messageText = string.Empty;  
  5.   for (int i = 0; i < sheet1.Rows.Count; i++) {  
  6.    UserCode1 = sheet1.Rows[i]["UserCode"].ToString();  
  7.    string sp = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;  
  8.    SqlConnection con = new SqlConnection(sp);  
  9.    con.Open();  
  10.    SqlCommand cmd = new SqlCommand("select UserCode from test where UserCode=@UserCode", con);  
  11.    cmd.CommandType = CommandType.StoredProcedure;  
  12.    cmd.Parameters.AddWithValue("@UserCode", UserCode1);  
  13.    SqlDataReader reader = cmd.ExecuteReader();  
  14.    if (reader.Read()) {  
  15.     label1.Text = "Data Already Exist";  
  16.     isValid = false;  
  17.    }  
  18.    reader.Close();  
  19.    cmd.Dispose();  
  20.    con.Close();  
  21.   }  
  22.   for (int b = 0; b < sheet2.Rows.Count; b++) {  
  23.    UserCode2 = sheet2.Rows[b]["UserCode"].ToString();  
  24.    string sp1 = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;  
  25.    SqlConnection conn1 = new SqlConnection(sp1);  
  26.    conn1.Open();  
  27.    SqlCommand sql1 = new SqlCommand("select UserCode from test where UserCode=@UserCode", conn1);  
  28.    sql1.CommandType = CommandType.StoredProcedure;  
  29.    sql1.Parameters.AddWithValue("@UserCode", UserCode2);  
  30.    SqlDataReader reader = sql1.ExecuteReader();  
  31.    if (reader.Read()) {  
  32.     label1.Text = "Data Already Exist";  
  33.     isValid = false;  
  34.    }  
  35.    reader.Close();  
  36.    conn1.Close();  
  37.   }  
  38.   return isValid;  
  39.   
  40.  } catch (Exception ex) {  
  41.   
  42.   throw ex;  
  43.  }  
  44. }  
  45.   
  46. protected void btnUpload_Click(object sender, EventArgs e) {  
  47.   if (!ValidateUserBO(DataTable sheet1, DataTable sheet2) ValidFile = false;  
  48.   }  

Answers (2)