Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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();
                //var result = 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);
                }
                // Now you have a list of arrays that you can iterate over
                foreach (string[] fields in employees)
                {
                    string id = fields[0];
                }

                myReader.Close();
                //result.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();
                    //Add Header          
                    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();
                    //Append Data
                    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();
                } 
            
            }
Posted
Updated 1-Sep-14 2:43am
v4

1 solution

Hi
Maybe these links might be of some use:


[^]

http://www.thewebbureau.com/export-datatable-to-csv[^]

Basically they export a database to excel or csv.

Thanks
Paul
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900