At first I tried this:
string set = "";
for (int i = 1; i < result.Count; i++)
{
if ((fieldtypes[i] == "System.Int32"))
{
set += fields[i] + "=" + result[i] + ", ";
}
else if (fieldtypes[i] == "System.String")
{
set += fields[i] + "='" + result[i] + "', ";
}
else if (fieldtypes[i] == "System.Boolean")
{
set += fields[i] + "=" + result[i] + ", ";
}
else if (fieldtypes[i] == "System.DateTime")
{
set += fields[i] + "='#" + System.DateTime.Now + "#', ";
}
}
set = set.Substring(0, set.Length - 2);
string sql11 = "UPDATE [Contacts] SET " + set + " WHERE pkContactID=" + cKey;
OleDbCommand myCommand11 = new OleDbCommand(sql11, myConnection);
myCommand11.ExecuteNonQuery();
Now this WORKED when I omitted the string and datetime conditions so that it only updated the int and boolean. So it has something to do with a syntax error when I try to update a field where the type is a string.
Then I heard that you have to use parameters when writing to an .mdb file, so I tried this:
string sql11 = "UPDATE [Contacts] SET ";
for (int i = 1; i < result.Count; i++)
{
sql11 += fields[i] + " = ?, ";
}
sql11 = sql11.Substring(0, sql11.Length - 2);
sql11 += " WHERE pkContactID = " + cKey;
using (myConnection)
{
using (OleDbCommand myCommand11 = new OleDbCommand(sql11, myConnection))
{
myCommand11.CommandType = CommandType.Text;
for (int j = 1; j < result.Count; j++)
{
if (fieldtypes[j] == "System.Int32")
{
myCommand11.Parameters.AddWithValue(fields[j], int.Parse(result[j]));
}
else if (fieldtypes[j] == "System.String")
{
myCommand11.Parameters.AddWithValue(fields[j], result[j]);
}
else if (fieldtypes[j] == "System.Boolean")
{
myCommand11.Parameters.AddWithValue(fields[j], Boolean.Parse(result[j]));
}
else if (fieldtypes[j] == "System.DateTime")
{
myCommand11.Parameters.AddWithValue(fields[j], DateTime.Now);
}
}
Console.WriteLine(sql11);
myCommand11.ExecuteNonQuery();
}
}
}
Which did not work either. I don't think the ?'s are being replaced properly.
Anyway, please help me fix it so that I can update properly.
Instead of having to mess around with the UPDATE query string for Access, which is easily prone to syntax errors, I just created a DataTable object and SELECTed the row I wanted to UPDATE. I then updated the table via an array of the elements that I wanted to change, then updated the table back to the server using an adapter. This worked out well without me worrying about the syntax! :)
Cheers
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a datatable with a few columns. The first column is our employee id. Unfortunately not all of the rows are numeric and we want to remove those who aren't numeric. For instance, we have 1 row which has "##$" and I want to remove rows like these. I currently have the following code.
var len = dt.Rows.Count;
for(int y = 0; y < len; y++)
{
var mwd = dt.Columns[0].ToString();
bool valid = int.TryParse(mwd, out int n);
if (valid)
{
log.LogInformation("mwd is numeric");
}
else
{
log.LogInformation("mwd is not numeric");
dt.Rows[y].Delete();
}
}
However, this doesn't remove the row. What am I doing wrong? Thanks in advance.
EDIT: Surrounding code
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string[] columns = { "Mwd", "Naam", "Kostenplaats Externe id (Klant)", "Kostenplaats Loonlijstcode (Activiteit)", "Kostenplaats Naam (Activiteit)", "Datum", "Uren ruw", "Ber. Uren", "Verlof volledig pad" };
foreach (string column in columns)
{
dt.Columns.Add(column);
}
using (StreamReader reader = new StreamReader(req.Body))
{
while (reader.EndOfStream == false)
{
string[] rows = reader.ReadLine().Split(',');
DataRow dr = dt.NewRow();
for (int i = 0; i < columns.Length; i++)
{
var temp = rows[i].Trim('"');
dr[i] = temp.Trim('\'');
}
dt.Rows.Add(dr);
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
foreach (DataColumn column in dt.Columns)
{
var mwd = dt.Rows[i][column].ToString();
int n;
bool valid = int.TryParse(mwd, out n);
if (valid)
{
log.LogInformation("mwd is numeric");
}
else
{
log.LogInformation("mwd is not numeric");
dt.Rows[i].Delete();
i--;
break;
}
}
}
dt.AcceptChanges();
log.LogInformation(dt.ToString());
for (int x = 0; dt.Rows.Count > x; x++)
{
string sql = "INSERT INTO dbo.kronos (Mwd, Naam, KostenplaatsExterneIdKlant, KostenplaatsLoonlijstcodeActiviteit, KostenplaatsNaamActiviteit, Datum, UrenRuw, BerUren, VerlofVolledigPad)" +
" VALUES ('" + dt.Rows[x]["Mwd"].ToString() + "', '" + dt.Rows[x]["Naam"].ToString() + "', '"
+ dt.Rows[x]["Kostenplaats Externe id (Klant)"].ToString() + "', '" + dt.Rows[x]["Kostenplaats Loonlijstcode (Activiteit)"].ToString() + "', '"
+ dt.Rows[x]["Kostenplaats Naam (Activiteit)"].ToString() + "', '" + dt.Rows[x]["Datum"].ToString() + "', '"
+ dt.Rows[x]["Uren ruw"].ToString() + "', '" + dt.Rows[x]["Ber. Uren"].ToString() + "', '" + dt.Rows[x]["Verlof volledig pad"].ToString() + "')";
var str = Environment.GetEnvironmentVariable("ConnectionString");
using (SqlConnection connection = new SqlConnection(str))
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
}
}
return result;
try this code
for (int i = 0; i< dt.Rows.Count;i++)
{
foreach (DataColumn column in dt.Columns)
{
var mwd = dt.Rows[i][column].ToString();
int n;
bool valid = int.TryParse(mwd, out n);
if (valid)
{
log.LogInformation("mwd is numeric");
}
else
{
log.LogInformation("mwd is not numeric");
dt.Rows[i].Delete();
i--;
break;
}
}
}
dt.AcceptChanges();
If you know the name of the column or its index, use the following code
for (int i = 0; i < dt.Rows.Count; i++)
{
var mwd = dt.Rows[i]["Name"].ToString();
//or---------------------------------
var mwd = dt.Rows[i][index].ToString();
int n;
bool valid = int.TryParse(mwd, out n);
if (valid)
{
log.LogInformation("mwd is numeric");
}
else
{
log.LogInformation("mwd is not numeric");
dt.Rows[i].Delete();
i--;
}
}
dt.AcceptChanges();
try
{
string CSVFilePathName = textBox4.Text;
for (int i = 0; i < CSVFilePathName.Length; i++)
{
if (CSVFilePathName[i] == '\\')
{
CSVFilePathName.Insert(i + 1, "\\");
}
}
if (string.IsNullOrWhiteSpace(textBox4.Text))
{
MessageBox.Show("Please Select a File");
}
else
{
int count = 0;
// your code here
// string CSVFilePathName = #"'" + textBox4.Text + "'";
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
for (int i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(new char[] { ',' });
for (int f = 0; f < Cols; f++)
{
q = "SELECT * from questions where main_section='" + Fields[0] + "' AND secondary_section='" + Fields[1] + "' AND tert_section='" + Fields[2] + "' AND question='" + Fields[3] + "' AND difficulty='" + Fields[4] + "'";
OleDbCommand cmdn = new OleDbCommand(q, conn);
//MessageBox.Show(q);
object obj = cmdn.ExecuteScalar();
if (obj == null)
{
q = "insert into questions values('" + Fields[0] + "','" + Fields[1] + "','" + Fields[2] + "','" + Fields[3] + "' ,'" + Fields[4] + "')";
OleDbCommand cmdn1 = new OleDbCommand(q, conn);
cmdn1.ExecuteNonQuery();
}
else
{
count++;
}
//MessageBox.Show(Fields[f]);
}
}
// dataGridClients.DataSource = dt;
string msg = "Upload successful\n";
if (count > 0)
{
msg=count.ToString()+" Questions missed due to their duplicates in the database.";
}
MessageBox.Show(msg);
}
}
catch (Exception ex)
{
MessageBox.Show("Error is " + ex.ToString());
throw;
}
I am using c# winform to upload a csv file to my ms access db, but it is givig the error "The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data." What should do now?
I suggest specifying the table fields in the SQL like
INSERT INTO questions (fieldname1, fieldname2, ...) VALUES (...)
Use parameterized-values rather than writing the values directly in the string. This also allows you to specify the datatype and then the ADO.Net OLE adapter will hopefully handle it appropriately and get the long text inserted with no trouble. Go to Read / Write BLOBs ... for an example inserting BLOBS. The concept and code example are very relevant to inserting Long Text values. It demonstrates how to set up parameters for the query. In your case, use OleDbType.LongVarWChar for the Long Text fields.
I need help for my If Statement, at first it registers into the DB but only the value of 8, even if multiple values are tick. I know my if statements are wrong, but i have no idea what else to do. So should I remove the use another way if it's possible or I just messed a code up? I'm really bad at If's statement.
String points = null;
String ServiceCarWash = "Not Booked";
String ServiceCarPolish = "Not Booked";
String ServiceCarWax = "Not Booked";
int CustomerID = 0;
private void btnBook_Click_1(object sender, EventArgs e)
{
try
{
connection.Open();
String query = "INSERT into Booking ([Time],[Date],[CID],[VehicleNumber],[CarWash],[CarPolish],[CarWax]) VALUES('" + cmbTime.Text + "','" + dateTimePicker1.Text + "','" + CustomerID + "','" + txtVn.Text + "','" + ServiceCarWash + "','" + ServiceCarPolish + "','" + ServiceCarWax + "')";
OleDbCommand command = new OleDbCommand(query);
command.Connection = connection;
command.ExecuteNonQuery();
if (CbCarwash.Checked)
{
ServiceCarWash = "Booked";
}
if (CbCarPolish.Checked)
{
ServiceCarPolish = "Booked";
}
if (CbCarWax.Checked)
{
ServiceCarWax = "Booked";
}
{
if (txtMember.Text.Equals("VIP"))
{
if (ServiceCarPolish == "Booked")
{
points = "20";
}
if (ServiceCarWash == "Booked")
{
points = "2";
}
if (ServiceCarWax == "Booked")
{
points = "8";
}
}
else if (txtMember.Text.Equals("Walk-In"))
{
if (ServiceCarPolish == "Booked")
{
points = "0";
}
if (ServiceCarWash == "Booked")
{
points = "0";
}
if (ServiceCarWax == "Booked")
{
points = "0";
}
}
string query1 = "UPDATE *Customer set Points='" + points + "'";
OleDbCommand command1 = new OleDbCommand(query1);
command1.Connection = connection;
command1.ExecuteNonQuery();
MessageBox.Show("Your time has been booked.");
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
How to accumulate points in code as per comments on previous question. See comments in the below and look out for the += signs.
//String points = null;
int points = 0;
String ServiceCarWash = "Not Booked";
String ServiceCarPolish = "Not Booked";
String ServiceCarWax = "Not Booked";
int CustomerID = 0;
private void btnBook_Click_1(object sender, EventArgs e)
{
try
{
connection.Open();
String query = "INSERT into Booking ([Time],[Date],[CID],[VehicleNumber],[CarWash],[CarPolish],[CarWax]) VALUES('" + cmbTime.Text + "','" + dateTimePicker1.Text + "','" + CustomerID + "','" + txtVn.Text + "','" + ServiceCarWash + "','" + ServiceCarPolish + "','" + ServiceCarWax + "')";
OleDbCommand command = new OleDbCommand(query);
command.Connection = connection;
command.ExecuteNonQuery();
if (CbCarwash.Checked)
{
ServiceCarWash = "Booked";
}
if (CbCarPolish.Checked)
{
ServiceCarPolish = "Booked";
}
if (CbCarWax.Checked)
{
ServiceCarWax = "Booked";
}
{
if (txtMember.Text.Equals("VIP"))
{
if (ServiceCarPolish == "Booked")
{
points += 20;
}
if (ServiceCarWash == "Booked")
{
points += 2;
}
if (ServiceCarWax == "Booked")
{
points += 8;
}
}
else if (txtMember.Text.Equals("Walk-In"))
{
if (ServiceCarPolish == "Booked")
{
points += 0;
}
if (ServiceCarWash == "Booked")
{
points += 0;
}
if (ServiceCarWax == "Booked")
{
points += 0;
}
}
//string query1 = "UPDATE *Customer set Points='" + points + "'";
string query1 = "UPDATE *Customer set Points='" + points.ToString() + "'";
OleDbCommand command1 = new OleDbCommand(query1);
command1.Connection = connection;
command1.ExecuteNonQuery();
MessageBox.Show("Your time has been booked.");
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
Set your fields before inserting in to the database.
Move this code to the end of the method (or to just before the Customer update):
connection.Open();
String query = "INSERT into Booking ([Time],[Date],[CID],[VehicleNumber],[CarWash],[CarPolish],[CarWax]) VALUES('" + cmbTime.Text + "','" + dateTimePicker1.Text + "','" + CustomerID + "','" + txtVn.Text + "','" + ServiceCarWash + "','" + ServiceCarPolish + "','" + ServiceCarWax + "')";
OleDbCommand command = new OleDbCommand(query);
command.Connection = connection;
command.ExecuteNonQuery();
Also, is *Customer the right table name, should it not just be Customer?
Check the value of your checkboxes to see if they are coming through correctly, then step through your IF's to make sure they calculate correctly. Refactoring in to smaller methods may help you break down the problem so that it is less complex and easier to work with.
Also check your brackets - you have some crazy stuff going on. See the second to last bracket below and you also have something weird going on a bit higher up with a bracket the wrong way around.
else if (txtMember.Text.Equals("Walk-In"))
{
if (ServiceCarPolish == "Booked")
{
points = "0";
}
if (ServiceCarWash == "Booked")
{
points = "0";
}
if (ServiceCarWax == "Booked")
{
points = "0";
}
**{**
}
Also make sure txtMember.Text always has a value so that points are set. Depending on your input, you can make your code a bit more robust by comparing tolower() in strings, e.g.: txtMember.Text.ToLower().Equals("walk-in").
And step through with the debugger! :)
We have a data table with 2500 columns and 2000 rows. When we try to export using OLEDB i am getting an error "too many fields defined". I cant use Excel Inter op, since it consumes more time. Is there any other way to fix this issue
using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + "Extended Properties=" + "\"" + "Excel 8.0;HDR=NO;" + "\""))
{
con.Open();
OleDbCommand cmdInsert;
if (createTable || !File.Exists(fileName))
{
sql = "CREATE TABLE " + tableName + " (";
for (int i = 0; i < tbl.Columns.Count; i++)
{
sql += "[" + tbl.Columns[i].ColumnName + "]";
if (i + 1 == tbl.Columns.Count) //Here we decide should we close insert command or appebd another create column command
sql += " " + GetColumnType(tbl.Columns[i]) + ")"; //Close insert
else
sql += " " + GetColumnType(tbl.Columns[i]) + ","; //there is more columns to add
}
}
if (!String.IsNullOrEmpty(sql))
{
cmdInsert = new OleDbCommand(sql, con);
cmdInsert.ExecuteNonQuery();
}
foreach (DataRow row in tbl.Rows)
{
//Dodati parametre na comandu
string values = "(";
for (int i = 0; i < tbl.Columns.Count; i++)
{
if (i + 1 == tbl.Columns.Count)
{
if (tbl.Columns[i].DataType == System.Type.GetType("System.Decimal") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Int64") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Double"))
values += String.IsNullOrEmpty(row[i].ToString()) ? "0)" : Convert.ToDecimal(row[i]).ToString("#0.00", _infoEn) + ")";
else
values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "')";
//values += "'" + "test" + "')";
}
else
{
if (tbl.Columns[i].DataType == System.Type.GetType("System.Decimal") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Int64") ||
tbl.Columns[i].DataType == System.Type.GetType("System.Double"))
values += String.IsNullOrEmpty(row[i].ToString()) ? "0," : Convert.ToDecimal(row[i]).ToString("#0.00", _infoEn) + ",";
else
values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "',";
//values += "'" + "test" + "',";
}
}
string sqlInsert = String.Format("Insert into [{0}$] VALUES {1}", tableName, values);
cmdInsert = new OleDbCommand(sqlInsert, con);
cmdInsert.ExecuteNonQuery();
}
}
I found out that the limitation of using oledb is 255 columns. Hence i am converting the datatable into csv using the below code and write it into response of the http page
public void CreateCSVFile(System.Data.DataTable dt, out StringWriter stw)
{
stw = new StringWriter();
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
stw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
stw.Write(",");
}
}
stw.Write(stw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
stw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
stw.Write(",");
}
}
stw.Write(stw.NewLine);
}
}
now i am able to fix the issue of exporting huge data
Does Excel (which you are connecting too) have a limit of columns it supports via a "table" definition? that might be your bottleneck.
What I would suggest, is when building your table out, you do it for all the columns. I would suggest running it in batch cycles. Call the function and do 100 columns at a time and export only 2 or 3 rows. So, it may take 20 loop cycles to build out all 2000 columns, but you might find your break there. If you DO get to the full 2000 columns, then expand the rows being exported and find out at what ROW inserted it fails. It could be there are some NULL values floating around killing your process.
I have a listview that has 2 columns and a checkbox.
What i'm doing is if the checkbox is true, I want to check the text from columns 1 and 2 and make it a string. This is what I have.
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
listView1.Items.RemoveAt(i);
string sql = "uscolumn = '" + listView1.Items[i].ToString() + "' and ukcolumn = '" + listView1.Items[i].ToString() + "'";
}
}
The above code doesn't work but i'm not sure which way to go with it, the .Check works as intended but the strings dont.
so in this example:
CheckBox|column1|Column2
True|Fruit|Apples
usColumn = "Fruit"
ulColumn = "Apples"
Here is my suggestion for you:
int i = 0;
while (i < listView1.Items.Count)
{
if (listView.Items[i].Checked)
{
string sql = "uscolumn = '" + listView1.Items[i].SubItems[0].Text + "' and ukcolumn = '" + listView1.Items[i].SubItems[1].Text + "'";
listView.Items.RemoveAt(i);
}
else
{
i++;
}
}
I think you need to use Items[i].Text rather than ToString():
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
listView1.Items.RemoveAt(i);
string sql = "uscolumn = '" + listView1.Items[i].Text + "' and ukcolumn = '" + listView1.Items[i].Text + "'";
}
}
To delete the rows safly, you can save the item name in a array and then remove them after you go through the whole list.
This assumes that you use unique names for each one, if the Name properie is not used at all you can add a Name specificly for the deletion.
List<string> toDelete = new List<int>;
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
toDelete.Add(listView1.Items[0].Name);
string sql = "uscolumn = '" + listView1.Items[i].Text + "' and ukcolumn = '" + listView1.Items[i].Text + "'";`
}
}
foreach(string name in toDelete)
listView1.ItemsRemoveByKey(name);
Another way to do it would be to remove 1 from i each time you delete.
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
string sql = "uscolumn = '" + listView1.Items[i].Text + "' and ukcolumn = '" + listView1.Items[i].Text + "'";
listView1.Items.RemoveAt(i--);
}
}