sqlite 3 error in logic or missing database in - c#

I think its a logic error as I can access my local database fine. My error code looks like this:
SQL logic error or missing database
SQLiteConnection myConnection = new SQLiteConnection();
myConnection.ConnectionString = #"Data Source= C:\Users\Lewis Elliott\Desktop\banklist.sqlite3";
using (myConnection)
{
string SQL = "INSERT INTO recipes (Index, name, serves, description, information, method_1, method_2, key, cost, calories, carbs, fat, protein, sodium, sugar, type, is_populated) VALUES";
SQL += "(#Index, #name, #serves, #description, #information, #method_1, #method_2, #key, #cost, #calories, #carbs, #fat, #protein, #sodium, #sugar, #type, #is_populated)";
SQLiteCommand cmd = new SQLiteCommand(SQL);
cmd.Connection = myConnection;
cmd.Parameters.AddWithValue("#Index", Guid.NewGuid());
cmd.Parameters.AddWithValue("#name", nameBox.Text);
cmd.Parameters.AddWithValue("#serves", servesBox.Text);
cmd.Parameters.AddWithValue("#description", descriptionBox.Text);
cmd.Parameters.AddWithValue("#information", informationBox.Text);
cmd.Parameters.AddWithValue("#method_1", method1Box.Text);
cmd.Parameters.AddWithValue("#method_2", method2Box.Text);
cmd.Parameters.AddWithValue("#key", keyBox.Text);
cmd.Parameters.AddWithValue("#cost", costBox.Text);
cmd.Parameters.AddWithValue("#calories", caloriesBox.Text);
cmd.Parameters.AddWithValue("#carbs", carbsBox.Text);
cmd.Parameters.AddWithValue("#fat", fatbox.Text);
cmd.Parameters.AddWithValue("#protein", proteinBox.Text);
cmd.Parameters.AddWithValue("#sodium", sodiumBox.Text);
cmd.Parameters.AddWithValue("#sugar", sugarBox.Text);
cmd.Parameters.AddWithValue("#type", typeBox.Text);
cmd.Parameters.AddWithValue("#is_populated", isPopulatedBox.Text);
myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();
Can anyone see any errors in this logic?

Related

INSERT INTO statement is not working in C# , trying to insert into an Access database

I've been trying to insert data into a MS Access database and I can't see what is wrong with my insert statement. I checked all the values and they should be the same, I even tried a really simple statement with static values but it doesn't work anyways.
I've followed the syntax to the letter but I still keep getting an INSERT INTO error when I get to the cmd.ExecuteNonQuery() command.
string str = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\B8328\source\repos\KaihatsuEnshuu\KaihatsuEnshuu\OI21Database1.accdb";
OleDbConnection con = new OleDbConnection(str);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO order (orderEmpno, orderCustomerId) VALUES (#orderEmpno, #orderCustomerId)";
cmd.Parameters.AddWithValue("#orderEmpno", comboBox1.SelectedValue);
cmd.Parameters.AddWithValue("#orderCustomerId", comboBox2.SelectedValue);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("An order has been started");

Excel to DataTable Insert

I am trying to insert into a database from Excel, and I've got all values from Excel. When I insert into my database I get Factal Exception. How do I recover from this exception?
MySqlCommand commd = new MySqlCommand();
commd.Connection = OpenConnection();
commd.CommandText = "INSERT INTO sqa_tracking(IID,STARTDATE,ENDDATE,WEEK,SUPPLIER,LINENO,ORDER_ID,BRCARID,PAGE_FIRST,PAGE_LAST,PAGE_COUNT,ARTICLE_NO,COUNT_LINENO,TOTAL_NR_OF_ITEMS,CAR_SUPPLIER,CONTENT_PROVIDER_NAME,MANIFESTATION,FACTOR,UNITS,DATE_OF_SUBMISSION,UNITS_KEY,COUNT_UNITS_KEY,TOTAL_NR_OF_UNITS,ERRORS,KPI_ERRORS,OBII_ELEMENT,CAR_FIELD_NAME,ERROR_TYPE,WRONG_CAPTURE_IN_FILE,CORRECT_CAPTURE_WOULD_BE,REPEATING_IN_CAR,SOURCE_FILE_TYPE,FULL_AUTOM_CONV,ERROR_IN_SOURCE_FILE,ROOT_CAUSE_OF_THE_ERROR,PREVENTIVE_MEASURES,DATE_OF_IMPLEMENTATION,DATE_OF_TRANSMISSION,VALIDATED) VALUES (#IID,#STARTDATE,#ENDDATE,#WEEK,#SUPPLIER,#LINENO,#ORDER_ID,#BRCARID,#PAGE_FIRST,#PAGE_LAST,#PAGE_COUNT,#ARTICLE_NO,#COUNT_LINENO,#TOTAL_NR_OF_ITEMS,#CAR_SUPPLIER,#CONTENT_PROVIDER_NAME,#MANIFESTATION,#FACTOR,#UNITS,#DATE_OF_SUBMISSION,#UNITS_KEY,#COUNT_UNITS_KEY,#TOTAL_NR_OF_UNITS,#ERRORS,#KPI_ERRORS,#OBII_ELEMENT,#CAR_FIELD_NAME,#ERROR_TYPE,#WRONG_CAPTURE_IN_FILE,#CORRECT_CAPTURE_WOULD_BE,#REPEATING_IN_CAR,#SOURCE_FILE_TYPE,#FULL_AUTOM_CONV,#ERROR_IN_SOURCE_FILE,#ROOT_CAUSE_OF_THE_ERROR,#PREVENTIVE_MEASURES,#DATE_OF_IMPLEMENTATION,#DATE_OF_SUBMISSION,#VALIDATED) ";
commd.Parameters.AddWithValue("#STARTDATE", StartDate);
commd.Parameters.AddWithValue("#ENDDATE", EndDate);
commd.Parameters.AddWithValue("#WEEK", Week);
commd.Parameters.AddWithValue("#SUPPLIER", Supplier);
commd.Parameters.AddWithValue("#LINENO", LineNo);
commd.Parameters.AddWithValue("#ORDER_ID", ORDERID);
commd.Parameters.AddWithValue("#BRCARID", "");
commd.Parameters.AddWithValue("#PAGE_FIRST", PageFirst);
commd.Parameters.AddWithValue("#PAGE_LAST", Pagelast);
commd.Parameters.AddWithValue("#PAGE_COUNT", Pagecount);
commd.Parameters.AddWithValue("#ARTICLE_NO", ArticleNo);
commd.Parameters.AddWithValue("#COUNT_LINENO", COUNTLineNo);
commd.Parameters.AddWithValue("#TOTAL_NR_OF_ITEMS", Totalnrofitems);
commd.Parameters.AddWithValue("#CAR_SUPPLIER", CARSupplier);
commd.Parameters.AddWithValue("#CONTENT_PROVIDER_NAME", ContentProvidename);
commd.Parameters.AddWithValue("#MANIFESTATION", Manifestation);
commd.Parameters.AddWithValue("#FACTOR", AU);
commd.Parameters.AddWithValue("#UNITS", Units);
commd.Parameters.AddWithValue("#DATE_OF_SUBMISSION", Dateoftransmission);
commd.Parameters.AddWithValue("#UNITS_KEY", unitskey);
commd.Parameters.AddWithValue("#COUNT_UNITS_KEY", COUNTunitskey);
commd.Parameters.AddWithValue("ERRORS", Errors);
commd.Parameters.AddWithValue("#KPI_ERRORS", KPIErrors);
commd.Parameters.AddWithValue("#OBII_ELEMENT", OBIIElement);
commd.Parameters.AddWithValue("#CAR_FIELD_NAME", CARFieldname);
commd.Parameters.AddWithValue("#ERROR_TYPE", Errortype);
commd.Parameters.AddWithValue("#IID", IID);
commd.Parameters.AddWithValue("#WRONG_CAPTURE_IN_FILE", Wrongcaptureinfile);
commd.Parameters.AddWithValue("#CORRECT_CAPTURE_WOULD_BE", Correctcapturewouldbe);
commd.Parameters.AddWithValue("#REPEATING_IN_CAR", RepeatinginCAR);
commd.Parameters.AddWithValue("#SOURCE_FILE_TYPE", Sourcefiletype);
commd.Parameters.AddWithValue("#FULL_AUTOM_CONV", FulAutomConv);
commd.Parameters.AddWithValue("#ERROR_IN_SOURCE_FILE", Errorinsourcefile);
commd.Parameters.AddWithValue("#ROOT_CAUSE_OF_THE_ERROR", RootCauseoftheError);
commd.Parameters.AddWithValue("#PREVENTIVE_MEASURES", PreventiveMeasures);
commd.Parameters.AddWithValue("#DATE_OF_IMPLEMENTATION", DateofImplementation);
commd.Parameters.AddWithValue("#VALIDATED", Validated);
commd.ExecuteNonQuery();`
I used inline query and I got the parameter values from DataTable.
Always remember that fatal error occurred during command execution only because of incorrect parameters value name.
so correct the parameter value to avoid fetal errors during command execution
You have not provided parameter value for TOTAL_NR_OF_UNITS & DATE_OF_TRANSMISSION.
Also Please follow the order in your insert query and while adding parameters, makes it easier to troubleshoot.
commd.Parameters.AddWithValue("#TOTAL_NR_OF_UNITS",TotalNrOfUnits);
commd.Parameters.AddWithValue("#DATE_OF_TRANSMISSION", DateOfTransmission);
Either this . . .
SqlCommand cmd = new SqlCommand("insert into tbl_insert values (#id,#name,#Email,#City)");
cmd.Parameters.AddWithValue("#id", Convert.ToInt32(txtId.Text));
cmd.Parameters.AddWithValue("#name", txtName.Text);
cmd.Parameters.AddWithValue("#Email", txtEmail.Text);
cmd.Parameters.AddWithValue("#City", txtboxCity.Text);
...or this...
con.Open();
SqlCommand cmd = new SqlCommand(#"insert into tbl_insert values(#name,#email,#add)", con);
cmd.Parameters.AddWithValue("#name", txtname.Text);
cmd.Parameters.AddWithValue("#email", txtemail.Text);
cmd.Parameters.AddWithValue("#add", txtadd.Text);
cmd.ExecuteNonQuery();
con.Close();
...should work fine.

Insert statement into SQL Server db

I'm developing an ASP.NET MVC Web Application using SQL Server.
I am trying to INSERT a new entry into my database and I don't understand what am I doing wrong.
I get an exception on the line:
command.ExecuteNonQuery();
The code is:
try
{
SqlConnection connection = new SqlConnection(#"Data Source=.\SQLEXPRESS;Initial Catalog=UniversityManager;Integrated Security=True");
using (connection)
{
//SqlCommand command = new SqlCommand(
// "INSERT INTO Students VALUES(#Id, #Name, #Surname, #Year, #PhoneNumber, #Cnp);",
// connection);
connection.Open();
String sql = "INSERT INTO Students(Id,Name,Surname,Year,PhoneNumber,Cnp) " +
"VALUES (#Id, #Name, #Surname, #Year, #PhoneNumber, #Cnp)";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add("#Id", SqlDbType.Int);
command.Parameters["#Id"].Value = 5;
command.Parameters.Add("#Name", SqlDbType.VarChar);
command.Parameters["#Name"].Value = collection.Name;
command.Parameters.Add("#Surname", SqlDbType.VarChar);
command.Parameters["#Surname"].Value = collection.Surname;
command.Parameters.Add("#Year", SqlDbType.Int);
command.Parameters["#Year"].Value = collection.Year;
command.Parameters.Add("#PhoneNumber", SqlDbType.VarChar);
command.Parameters["#PhoneNumber"].Value = collection.PhoneNumber;
command.Parameters.Add("#Cnp", SqlDbType.VarChar);
command.Parameters["#Cnp"].Value = collection.Cnp;
command.ExecuteNonQuery();
connection.Close();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Thank you!
YEAR is a reserved keyword for Sql Server. So, if you really have a column with that name, then you need to enclose it in square brackets every time you refer to it. Better change that name
String sql = "INSERT INTO Students(Id,Name,Surname,[Year],PhoneNumber,Cnp) " +
"VALUES (#Id, #Name, #Surname, #Year, #PhoneNumber, #Cnp)";
Another possibility is the Id column. If this column has the IDENTITY property set to true, then you should not set a value for it. It is automatically calculated by the database engine.
Looking at your innerexception message, it seems the problem is due to one or more of your parameters contains more text than allowed by the database field size.
You could try something like this (for each varchar parameter)
// Assuming the Name field is defined as varchar(15)
command.Parameters.Add("#Name", SqlDbType.VarChar, 15);
command.Parameters["#Name"].Value = collection.Name;
The String or binary data would be truncated exception means you're trying to insert a value that is too large for one of the columns in your Student table. For example, your Name field has a maximum length of 10 but you're trying to insert a 15 character name.
Check the values you're inserting and see if they're too large for the columns.

Inserting date in database

datofreg is of type datetime
my command is
SqlCommand cmd = new SqlCommand("insert into stud(datofreg) values('"+DateTime.Now+"'",conn);
when i execute it says Incorrect syntax near '1/6/2014 12:45:17 AM'.
Your problem is you are missing ) at the end of your query but don't use this way..
"insert into stud(datofreg) values('" + DateTime.Now + "')"
^^here
You should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.
For example;
SqlCommand cmd = new SqlCommand("insert into stud(datofreg) values(#date)", conn);
cmd.Parameters.AddWithValue("#date", DateTime.Now);
cmd.ExecuteNonQuery();

c# register form mysql insert

So im having problem gettin some data in to the database.. Im really stuck, im quite new to c# and have not learned all keywords yet, im not getting any errors just some nothing adds to my database.
textBox2.Text = myPWD;
MySqlConnection conn = new MySqlConnection("test")
string Query = "INSERT INTO `users`.`coffekeys` (`koffekeys`) VALUES ('values = #val')";
MySqlCommand data = new MySqlCommand(Query, conn);
MySqlDataReader myReader;
conn.Open();
SelectCommand.Parameters.AddWithValue("#val", this.textBox2.Text);
conn.Closed()
Manipulate the concatenation of value in passing of parameters. Don't do it inside sql statement.
string Query = "INSERT INTO `users`.`coffekeys` (`koffekeys`) VALUES (#val)";
// other codes
SelectCommand.Parameters.AddWithValue("#val", "values = " + this.textBox2.Text);
the reason why the parameter is not working is because it was surrounded by single quotes. Parameters are identifiers and not string literals.
The next problem is you did not call ExecuteNonQuery() which will execute the command.
Before closing the connection, call ExecuteNonQuery()
// other codes
data.ExecuteNonQuery();
conn.Close();
You should Google around and you will receive lots of content
You need to run ExecuteNonQuery
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand(
"insert into st (ID,Name) values ('11','seed');", con);
cmd.ExecuteNonQuery();
cmd.Close();

Categories

Resources