I am passing values from array list to my sql Query as Parameter. but it throws error as
"No mapping exists from object type System.String[] to a known managed provider native type."
Below is my code.
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString);
string sqlconnection = "select EMP_CODE from Emp_Login where EMP_MGR_CODE='" + lblUsername.Text + "'";
connection.Open();
SqlCommand selectcmd = new SqlCommand(sqlconnection, connection);
SqlDataReader myReader = selectcmd.ExecuteReader();
List<string[]> employees = new List<string[]>();
while (myReader.Read())
{
string[] fields = new string[1];
fields[0] = myReader["EMP_CODE"].ToString();
employees.Add(fields);
}
foreach (string[] fields in employees)
{
string id = fields[0];
}
myReader.Close();
connection.Close();
query = "select a.UserId as EmpCode,b.EmployeeName,CONVERT(VARCHAR(10),a.LogDate,110) as AccessDate," +
" min(CONVERT(VARCHAR(10),a.LogDate,108)) as InTime,max(CONVERT(VARCHAR(10),a.LogDate,108)) as OutTime," +
" convert(varchar(8),(convert(datetime,max(a.LogDate),110) - convert(datetime,MIN(a.Logdate),110)),108) AS TotalHours" +
" from DeviceLogs as a,Employees as b where Cast(a.LogDate AS DATE) between '" + startdate + "' and '" + enddate + "' and a.UserId IN ({0}) and" +
" a.UserId=b.EmployeeCode group by b.EmployeeName,a.UserId,CONVERT(VARCHAR(10),a.LogDate,110)";
string[] paramNames = employees.Select((s, i) => "@EMP_CODE" + i.ToString()).ToArray();
cn.Open();
string inClause = string.Join(",", paramNames);
using (var command = new SqlCommand(string.Format(query, inClause), cn))
{
for (int i = 0; i < paramNames.Length; i++)
{
command.Parameters.AddWithValue(paramNames[i], employees[i]);
}
SqlDataReader dr = command.ExecuteReader();
StringBuilder sb = new StringBuilder();
for (int count = 0; count < dr.FieldCount; count++)
{
if (dr.GetName(count) != null)
sb.Append(dr.GetName(count));
if (count < dr.FieldCount - 1)
{
sb.Append(",");
}
}
Response.Write(sb.ToString() + "\n");
Response.Flush();
while (dr.Read())
{
sb = new StringBuilder();
for (int col = 0; col < dr.FieldCount - 1; col++)
{
if (!dr.IsDBNull(col))
sb.Append(dr.GetValue(col).ToString().Replace(",", " "));
sb.Append(",");
}
if (!dr.IsDBNull(dr.FieldCount - 1))
sb.Append(dr.GetValue(
dr.FieldCount - 1).ToString().Replace(",", " "));
Response.Write(sb.ToString() + "\n");
Response.Flush();
}
dr.Dispose();
}
}