I am importing data from excel to mySql DB using OLEDB provider. When there is a field with %, it automatically get divided by 100. Let say I have 2 column Qty and Disc_Percentage with value 4 and 25%. When I got data from excel sheet to datatable, it convert Disc_Percentage value to 0.25. I have below code for reading excel
public static DataTable ConvertExcelFileDataToDataTable(string file, string extension)
{
var dtImportedData = new DataTable();
// -- Start of Constructing OLEDB connection string to Excel file
var props = new Dictionary<string, string>();
// For Excel 2007/2010
if (file.ToLower().EndsWith(".xlsx"))
{
props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
props["Extended Properties"] = "\"Excel 12.0\"";
}
// For Excel 2003 and older
else if (file.ToLower().EndsWith(".xls"))
{
props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
props["Extended Properties"] = "\"Excel 8.0;IMEX=1;HDR=Yes\"";
}
else
return null;
props["Data Source"] = file;
var sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
sb.Append(prop.Key);
sb.Append('=');
sb.Append(prop.Value);
sb.Append(';');
}
//You must use the $ after the object you reference in the spreadsheet
var conn = new OleDbConnection(sb.ToString());
conn.Open();
var myCommand = new OleDbDataAdapter();
DataTable dtSerialNumbers = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dtSerialNumbers == null)
{
return dtImportedData;
}
var excelSheets = new String[dtSerialNumbers.Rows.Count];
// Add the sheet name to the string array.
if (dtSerialNumbers.Rows.Count > 0)
{
//we need the first sheet so save the first sheet name from the first row of the table
excelSheets[0] = dtSerialNumbers.Rows[0]["TABLE_NAME"].ToString();
myCommand = new OleDbDataAdapter("SELECT * FROM [" + excelSheets[0] + "]", conn);
}
myCommand.Fill(dtImportedData);
dtImportedData.TableName = excelSheets[0].Replace("$", String.Empty);
return dtImportedData;
}
Excel(2016 on my machine) autodetects entered '%' and sets the cell-format to percent. I suggest reformating those cells before importing.
Related
I am getting data from excel and showing it in DataGridWiew.
I have two textboxes, one is for starting index for first record and other is for last record.
Code works fine. But lets suppose starting record is 1 and ending is 10 when I change 10 to 1 or 2 it gives me an error in this line:
adapter.Fill(dataTable);
Full Code is below:
public DataSet Parse(string fileName)
{
string connectionString = string.Format("provider = Microsoft.Jet.OLEDB.4.0; data source = {0}; Extended Properties = Excel 8.0;", fileName);
DataSet data = new DataSet();
foreach (var sheetName in GetExcelSheetNames(connectionString))
{
using (OleDbConnection con = new OleDbConnection(connectionString))
{
string query = "";
var dataTable = new DataTable();
if(tbStarting.Text.Trim()=="" && tbEnding.Text.Trim() == "")
{
query = string.Format("SELECT * FROM [{0}]", sheetName);
}
else
{
query = string.Format("SELECT * FROM [{0}] where SrNo between " + int.Parse(tbStarting.Text.Trim()) + " and " + int.Parse(tbEnding.Text.Trim()) + " order by SrNo", sheetName);
}
con.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
adapter.Fill(dataTable);
data.Tables.Add(dataTable);
con.Close();
}
}
return data;
}
static string[] GetExcelSheetNames(string connectionString)
{
OleDbConnection con = null;
DataTable dt = null;
con = new OleDbConnection(connectionString);
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheetNames = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheetNames;
}
Why this is happening please help me?
Looking at the code, it seems that your procedure is working when you ask to retrieve all the record in each table. But you are not showing which table (Sheet) is actually used afterwars.
Chances are, you are using the first one only.
When you submit some parameters, only one of the tables (Sheets) can fulfill those requirements. The other(s) don't, possibly because a field named [SrNo] is not present.
This causes the More Parameters Required error when trying to apply a filter.
Not related to the error, but worth noting: you don't need to recreate the whole DataSet + DataTables to filter your DataSources.
The DataSet.Tables[N].DefaultView.RowFilter can be used to get the same result without destroying all the objects each time a filter is required.
RowFilter has some limitations in the language (e.g. does not support BETWEEN, Field >= Value1 AND Field <= Value2 must be used), but it's quite effective.
This is a possible setup:
(xDataSet is a placeholder for your actual DataSet)
//Collect the values in the TextBoxes in a string array
private void button1_Click(object sender, EventArgs e)
{
string[] Ranges = new string[] { tbStarting.Text.Trim(), tbEnding.Text.Trim() };
if (xDataSet != null)
FilterDataset(Ranges);
}
private void FilterDataset(string[] Ranges)
{
if (string.IsNullOrEmpty(Ranges[0]) & string.IsNullOrEmpty(Ranges[1]))
xDataSet.Tables[0].DefaultView.RowFilter = null;
else if (string.IsNullOrEmpty(Ranges[0]) | string.IsNullOrEmpty(Ranges[1]))
return;
else if (int.Parse(Ranges[0]) < int.Parse(Ranges[1]))
xDataSet.Tables[0].DefaultView.RowFilter = string.Format("SrNo >= {0} AND SrNo <= {1}", Ranges[0], Ranges[1]);
else
xDataSet.Tables[0].DefaultView.RowFilter = string.Format("SrNo = {0}", Ranges[0]);
this.dataGridView1.Update();
}
I've modified your code you code a bit to handle those requirements.
(I've left here those filters anyway; they're not used, but if you still want them, they are in a working condition)
DataSet xDataSet = new DataSet();
string WorkBookPath = #"[Excel WorkBook Path]";
//Query one Sheet only. More can be added if necessary
string[] WBSheetsNames = new string[] { "Sheet1" };
//Open the Excel document and assign the DataSource to a dataGridView
xDataSet = Parse(WorkBookPath, WBSheetsNames, null);
dataGridView1.DataSource = xDataSet.Tables[0];
dataGridView1.Refresh();
public DataSet Parse(string fileName, string[] WorkSheets, string[] ranges)
{
if (!File.Exists(fileName)) return null;
string connectionString = string.Format("provider = Microsoft.ACE.OLEDB.12.0; " +
"data source = {0}; " +
"Extended Properties = \"Excel 12.0;HDR=YES\"",
fileName);
DataSet data = new DataSet();
string query = string.Empty;
foreach (string sheetName in GetExcelSheetNames(connectionString))
{
foreach (string WorkSheet in WorkSheets)
if (sheetName == (WorkSheet + "$"))
{
using (OleDbConnection con = new OleDbConnection(connectionString))
{
DataTable dataTable = new DataTable();
if ((ranges == null) ||
(string.IsNullOrEmpty(ranges[0]) || string.IsNullOrEmpty(ranges[1])) ||
(int.Parse(ranges[0]) > int.Parse(ranges[1])))
query = string.Format("SELECT * FROM [{0}]", sheetName);
else if ((int.Parse(ranges[0]) == int.Parse(ranges[1])))
query = string.Format("SELECT * FROM [{0}] WHERE SrNo = {1}", sheetName, ranges[0]);
else
query = string.Format("SELECT * FROM [{0}] WHERE (SrNo BETWEEN {1} AND {2}) " +
"ORDER BY SrNo", sheetName, ranges[0], ranges[1]);
con.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
adapter.Fill(dataTable);
data.Tables.Add(dataTable);
};
}
}
return data;
}
static string[] GetExcelSheetNames(string connectionString)
{
string[] excelSheetNames = null;
using (OleDbConnection con = new OleDbConnection(connectionString))
{
con.Open();
using (DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
{
if (dt != null)
{
excelSheetNames = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
excelSheetNames[i] = dt.Rows[i]["TABLE_NAME"].ToString();
}
}
}
}
return excelSheetNames;
}
I am trying to grab cells in XLS spreadsheets, assign them to string arrays, then manipulate the data and export to multiple CVS files.
The trouble is the XLS spreadsheet contains information that is not relevant, useable data doesn't start till row 17 and columns have no headings with just the default Sheet1.
I have looked at related questions and tried figuring it out myself with no success. The following code to read the XLS kinda works but is messy to work with as the row lengths vary from one XLS file to another and it is automatically pulling empty columns and rows.
CODE
public static void xlsReader()
{
string fileName = string.Format("{0}\\LoadsAvailable.xls", Directory.GetCurrentDirectory());
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";" + #"Extended Properties='Excel 8.0;HDR=Yes;'";
string queryString = "SELECT * FROM [Sheet1$]";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
int counter = 0;
while (reader.Read())
{
Console.WriteLine("Line " + counter + ":" + reader[28].ToString()); // Just for testing
counter++;
}
}
}
I could do a bunch of trickery with loops to get the data that is required but there has to be a query string that could get the data from row 17 with only 8 columns(not 26 columns with 18 empty)?
I have tried many query string examples and can not seam to get any to work with a starting row index or filter out the empty data.
Here is a handy method that converts an excel file to a flat file.
You may want to change the connection string properties to suit your needs. I needed headers for my case.
Note you will need the Access database engine installed on your machine. I needed the 32 bit version since the app i dev'd was 32 bit. I bet you will also need it.
I parameterized the delimiter for the flat file, because I had cases where I didn't need a comma but a pipe symbol.
How to call method ex: ConvertExcelToFlatFile(openFileName, savePath, '|'); // pipe delimited
// Converts Excel To Flat file
private void ConvertExcelToFlatFile(string excelFilePath, string csvOutputFile, char delimeter, int worksheetNumber = 1)
{
if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath);
if (File.Exists(csvOutputFile)) throw new ArgumentException("File exists: " + csvOutputFile);
// connection string
var cnnStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml; IMEX=1; HDR=NO\"", excelFilePath);
var cnn = new OleDbConnection(cnnStr);
// get schema, then data
var dt = new DataTable();
try
{
cnn.Open();
var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (schemaTable.Rows.Count < worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");
string worksheet = schemaTable.Rows[worksheetNumber - 1]["table_name"].ToString().Replace("'", "");
string sql = String.Format("select * from [{0}]", worksheet);
var da = new OleDbDataAdapter(sql, cnn);
da.Fill(dt);
}
catch (Exception e)
{
throw e;
}
finally
{
// free resources
cnn.Close();
}
// write out CSV data
using (var wtr = new StreamWriter(csvOutputFile)) // disposes file handle when done
{
foreach (DataRow row in dt.Rows)
{
//MessageBox.Show(row.ItemArray.ToString());
bool firstLine = true;
foreach (DataColumn col in dt.Columns)
{
// skip the first line the initial
if (!firstLine)
{
wtr.Write(delimeter);
}
else
{
firstLine = false;
}
var data = row[col.ColumnName].ToString();//.Replace("\"", "\"\""); // replace " with ""
wtr.Write(String.Format("{0}", data));
}
wtr.WriteLine();
}
}
}
I am facing very strange issue. I have written class to which reads dbf file through oledb connection. I have downloaded dbf file from internet and it is reading all data correctly.
DBF file location: E:\Projects\SLAVE.DBF
I am facing following 2 issues
1) When I try to read other dbf file then it is reading only its table fields. it is not reading table fields data.
E:\Projects\line75.dbf
2) The other issue I am facing I have DBF files when I put these files in location then i am getting exception that
microsoft jet database engine does not find required object. Are you
missing some directive or path. E:\Projects\SDW_plnParcel.dbf
I am totally confused why it is reading SLAVE.DBF downloaded from internet correct, why it is not reading TABLE FIELDS DATA of line75.dbf and why it is throwing exception on SDW_plnParcel.dbf.
My class and one function for this class is as follows:
public class dbfHandler
{
public dbfHandler()
{
this.dbfTable = new DataTable();
}
public void initconnection(String filepath) // initialise dbconnection
{
String[] splitString = filepath.Split('\\');
this.filename = splitString[splitString.Length - 1];
splitString = splitString.Where(w => w != splitString[splitString.Length - 1]).ToArray();
String folderPath = String.Join("\\", splitString);
this.dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + folderPath + ";Extended Properties=dBase III");
this.dbConnection.Open();
}
public List<String> getcolumnvalues(int fieldIndex, List<int> rowIndexes)
{
List<String> columnvalues = new List<string>();
try
{
if(this.dbConnection.State == ConnectionState.Open)
{
string mySQL = "select * from " + this.filename; // dbf table name
OleDbCommand MyQuery = new OleDbCommand(mySQL, this.dbConnection);
OleDbDataReader reader = MyQuery.ExecuteReader();
int rowCount = 0;
while(reader.Read())
{
bool match = rowIndexes.Any(item => item == rowCount);
if(match == true)
{
String value = reader.GetValue(fieldIndex).ToString();
columnvalues.Add(value);
}
rowCount++;
}
reader.Close();
}
}
catch(Exception e)
{
throw e;
}
return columnvalues;
}
private String filename;
private DataTable dbfTable;
private OleDbConnection dbConnection;
}
When dealing with .DBF files, I have always had better results working with Microsoft's Visual Foxpro OleDb Provider
The connection string in simplified format
var connString = #"Provider=VFPOLEDB.1;Data Source=C:\SomePathToData;";
Now, instead of doing the data reader -- just to make sure you can get / see what you are expecting, try using a DataAdapter...
var da = new OleDataAdapter( yourSqlCmdObject, yourConnection)
var dt = new DataTable();
da.Fill(dt);
It should pull all columns from your query and all rows into proper data column types... Then you could cycle through all the column names, rows, etc..
foreach( DataColumn dc in dt.Columns )
var tmp = dc.ColumnName;
foreach( DataRow dr in dt.Rows )
{
object x = dr[0]; // get VALUE from column 0
x = dr["SpecificColumn"]; // if you KNOW the column name
}
Of which, you could tweak as needed. But if you only need a SPECIFIC column (or limited columns), change your query to quantify that.
Select OneField from YourTable...
I want to get excel sheet used data range by using oledb.
Code is below,
String strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=E:\\DOTNET\\CrsMicro\\CA.xls;"
+ "Extended Properties='Excel 8.0;HDR=Yes'";
using (OleDbConnection connExcel = new OleDbConnection(strExcelConn))
{
string selectString = "SELECT * FROM [CA$A1:D500]";
using (OleDbCommand cmdExcel = new OleDbCommand(selectString,connExcel))
{
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dt=new DataTable();
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = cmdExcel;
adp.FillSchema(dt, SchemaType.Source);
adp.Fill(dt);
int range=dt.Columns.Count;
int row = dt.Rows.Count;
//var result = cmdExcel.ExecuteReader();
//DataTable dtExcelSchema;
//dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// string excelsheetname = dtExcelSchema.Rows[0].ItemArray[2].ToString();
connExcel.Close();
//string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
}
}
my sheet range is not always A1:D500, it may vary frequently. So i need to get the sheet range dynamically. I know this can be achieved by interop, but i need to do it in oledb. Any suggestion?
create a named range:
https://msdn.microsoft.com/EN-US/library/office/ff196817.aspx
and replace selectString to
"SELECT * FROM [CA$MyNamedRange]"
Hi I'm also working on same kind of problem in OLEDB C# excel, I found below solution. It works for me. But I'm new to C#, I'm not sure how efficient it is.
But it is satisfying my requirements so far. This may be helpful for others.
I was able to get dynamic range in an excel sheet from a browsed input excel file (make sure excel file doesn't contain hidden sheets). This works perfectly for excel workbook containing single sheet. I haven't tested with multiple sheets.
Range: A [stat value]: Column Name[0] // Returns all rows from start value till the column name.
Example: A1:M0 // It will return all rows from A1 till column M. So here no need to worry how many rows you have in your excel.
Just by giving Column Name[0] takes all rows from the starting till the column M.
So '0' will be our outer range.
//Code under actual c# clas file where we are uploading excel.
Excel_Common excelComm = new Excel_Common(); // object to Excel_Common class file
string rangeStringwithSHeet =excelComm.GetSheetName(filepath).ToString().Trim('\'') + GetRange(excelComm.GetSheetName(filepath), excelComm.ExcelConn(filepath));
queryForExcelInput = string.Format("SELECT * FROM [{0}]", rangeStringwithSHeet);
Econ1 = new OleDbConnection(excelComm.ExcelConn(filepath));
Econ1.Open();
dataExcelInputTable = new DataTable();
OleDbCommand oleDbCommand1 = new OleDbCommand(queryForExcelInput, Econ1);
OleDbDataAdapter oleDbDaAdapter1 = new OleDbDataAdapter(oleDbCommand1);
oleDbDaAdapter1.Fill(dataExcelInputTable);
Excel_Common class file has below methods:
//Get Range like A4:M30
public string GetRange(string SheetName, string excelConnectionString)
{
string rangeInput = "",rangeColName="";
int columnsCount = 0;
int rowStartRange = 0;
columnsCount = GetNumberOfColumnsInSheet(SheetName, excelConnectionString);
rowStartRange = GetStartRowRange(SheetName, excelConnectionString); // This is optional if you want always A1. just assign 1 here
while (columnsCount > 0)
{
columnsCount--;
rangeColName = (char)('A' + columnsCount % 26) + rangeColName;
columnsCount /= 26;
}
rangeInput = "A" + rowStartRange + ":" + rangeColName + "0";
return rangeInput;
}
// Get Sheet Name assuming only one sheet for workbook and no hidden sheets
public string GetSheetName(string filepath)
{
string sheetname = "";
String connect = ExcelConn(filepath);
OleDbConnection con = new OleDbConnection(connect);
con.Open();
DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow row in tables.Rows)
{
sheetname = row[2].ToString();
if (!sheetname.EndsWith("$"))
continue;
}
con.Close();
return sheetname;
}
// Get number of columns in a given sheet
public int GetNumberOfColumnsInSheet(string SheetName, string excelConnectionString)
{
int columnsCount = 0;
//If a valid excel file
if (!string.IsNullOrEmpty(excelConnectionString))
{
using (OleDbConnection conn = new OleDbConnection(excelConnectionString))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);
if (dt.Rows.Count > 0)
columnsCount = dt.AsEnumerable().Where(a => a["TABLE_NAME"].ToString() == SheetName).Count();
conn.Close();
}
}
return columnsCount;
}
// Get the first row count in sheet contains some keyword . This method call is optional if you always want A1. Here I need to check some keyword exist and from there only I have to start something like A4
public int GetStartRowRange(string SheetName, string excelConnectionString)
{
int rowStartRange = 1;
//If a valid excel file
if (!string.IsNullOrEmpty(excelConnectionString))
{
using (OleDbConnection conn = new OleDbConnection(excelConnectionString))
{
string colValue;
conn.Open();
string cmdstr = "select * from [" + SheetName + "]";
OleDbCommand com = new OleDbCommand(cmdstr, conn);
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(com);
da.Fill(dt);
// get first row data where it started
foreach (DataRow dataRow in dt.Rows)
{
colValue = dataRow[0].ToString();
if ((colValue.Contains("Value1") || colValue.Contains("Value2") || colValue.Contains("Value3")) && (string.IsNullOrEmpty(dataRow[1].ToString()) == false))
{
rowStartRange = rowStartRange + 1;
break;
}
else
{
rowStartRange = rowStartRange + 1;
}
}
conn.Close();
}
}
return rowStartRange;
}
// Connection to excel document
public string ExcelConn(string FilePath)
{
string constr = "";
string extension = Path.GetExtension(FilePath);
//Checking for the extentions, if XLS connect using Jet OleDB
if (extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
constr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES\"", FilePath);
}
//Use ACE OleDb if xlsx extention
else if (extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
constr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES\"", FilePath);
}
return constr;
} // end of ExcelConn method
I have seen many examples of this around but something isn't working for me.
What I am looking to do is to read an Excel sheet, given a sheet and store those values into Lists.
For example, say I have an excel file that looks like:
First Second Third
f1 s1 t1
f2 s2 t2
f3 s3 t3
Each row is to be considered a set of values.
This is what I have doing so far:
List<string> ColumnNames= GetColumnNames();
using (OleDbConnection OleDbConn = new OleDbConnection(Path))
{
OleDbConn.Open();
String cmdString = "SELECT * FROM [" + sheetName+ "]";
OleDbCommand cmd = new OleDbCommand(cmdString, OleDbConn);
DataTable dt = new DataTable();
List<ValueSet> sets = new List<ValueSet>();
Dictionary<string, Value> values = new Dictionary <string,value>()
ValueSet valueset = new ValueSet(null);
using (OleDbDataReader oleRdr = cmd.ExecuteReader())
{
while (oleRdr.Read())
{
for (int i = 0; i < ColumnNames.Count; i++)
{
ColumnName cn = new ColumnName(columnNames[i]);
string data= oleRdr[f.Name].ToString();
Value value = new Value(data, f);
if (!values.ContainsKey(ColumnNames[i]))
{
values.Add(ColumnNames[i], value);
}
else
{
values[ColumnNames[i]] = value;
}
}
valueSet= new ValueSet(values);
sets.Add(valueSet);
}
return sets;;
}
I've gotten weird results with certain files using an OleDbConnection.
I suggest http://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F
With this you can read your CSV into a datatable and parse it into a list as follows:
DataTable dtPrereg;
using (GenericParserAdapter gp = new GenericParserAdapter(Server.MapPath("prereg.csv"), Encoding.UTF8))
{
gp.FirstRowHasHeader = true;
dtPrereg = gp.GetDataTable();
}
I haven't tested this on tab delimited files, but it should work the same (or you could convert your file to CSV)
If you really have a spreadsheet with a known number of named columns and you want to project them into a List<List<string>> it's a lot easier to just do it with Linq.
e.g.
List<List<string>> data;
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
data = (from row in rdr.Cast<DbDataRecord>()
select new List<string>
{
row["First"].ToString(),
row["Second"].ToString(),
row["Third"].ToString()
}).ToList();
}
try changing
ValueSet= new ValueSet(values);
sets.Add(ValueSet);
to
valueset = new ValueSet(values);
sets.Add(valueset );