I am trying to insert these values:
int limit = 50000;
int acc_id = 1;
string query = "INSERT INTO CURRENT_ACCOUNT(C-ACCOUNT_NO,DAILY_LIMIT)
VALUES ('"+acc_id+"','"+limit+"')";
OracleCommand command = new OracleCommand(query, con);
command.ExecuteNonQuery();
But getting a missing comma exception:
C# {"ORA-00917: missing comma"}
Are you sure your CURRENT_ACCOUNT table contains a column with the name C-ACCOUNT_NO? Is the column named C_ACCOUNT_NO (with the dash - replaced with an underscore _) instead?
If the column name genuinely does contain a dash, wrap the column name in double-quotes:
string query = "INSERT INTO CURRENT_ACCOUNT(\"C-ACCOUNT_NO\",DAILY_LIMIT) " + // ...
You have to add semicolon at the end of query..
string query = "INSERT INTO CURRENT_ACCOUNT(C-ACCOUNT_NO,DAILY_LIMIT)
VALUES ("+acc_id+","+limit+");";
Related
I want to insert the selected item index from a combobox into a tinyint column (FoodType) in SQL Server. I wrote the code below, but I get an exception:
Must declare the scalar variable #fType
What must I do?
string query = "INSERT INTO MenuTbl (FoodName,FoodType,FoodUnit, SalePrice)" +
"VALUES (#fName, #fType, #fUnit, #fPrice)";
connection = new SqlConnection(conectionString);
SqlCommand cmd = new SqlCommand(query,connection);
cmd.Parameters.AddWithValue("#fName", FNameTextBox.Text.Trim());
cmd.Parameters.AddWithValue("#fTyp", TypeComboBox.SelectedIndex + 1);
cmd.Parameters.AddWithValue("#funit", UnitComboBox.SelectedIndex +1);
cmd.Parameters.AddWithValue("#fprice", int.Parse(PriceEdit.Text));
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Are you missing an "e" on the parameter?
cmd.Parameters.AddWithValue("#fTyp", TypeComboBox.SelectedIndex + 1);
I think you made a typo with this line of code
cmd.Parameters.AddWithValue("#fTyp", TypeComboBox.SelectedIndex + 1);
It should be #fType atleast according to your select query
string query = "Insert into MenuTbl (FoodName,FoodType,FoodUnit, SalePrice)" +
"VALUES (#fName,#fType,#fUnit, #fPrice)";
And I also think that TypeComboBox.SelectedIndex + 1 will only give you the index+1 numerical value rather than the selected text contents. Is that what you want?
The Error i get is
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: No value given for one or more required parameters.
but this is when all parameters a present code bellow:
private OleDbDataReader dbReader;// Data Reader object
string sConnection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ICTSchool.accdb";
string sql;
OleDbConnection dbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ICTSchool.accdb");
OleDbCommand dbCommand;
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
private void bAdd_Click(object sender, EventArgs e)
{
{
dbConn = new OleDbConnection(sConnection);
dbConn.ConnectionString = sConnection;
dbConn.Open();
string code = (cBQualification.SelectedItem as ComboboxItem).Value.ToString();
string sqlinsert = "INSERT INTO Student VALUES (" + tBStudentNum.Text + "," + tBStudentName.Text+","+ tBCellNo.Text+","+ code + ")";
Console.WriteLine("Test 'sqlinsert' "+ sqlinsert);
dbCommand = new OleDbCommand(sqlinsert, dbConn);
dbCommand.ExecuteNonQuery();
}
}
Here is part of the article about how to insert values in MS Access.
To add one record to a table, you must use the field list to define which fields to put the data in, and then you must supply the data itself in a value list. To define the value list, use the VALUES clause.
For example, the following statement will insert the values "1", "Kelly", and "Jill" into the CustomerID, Last Name, and First Name fields, respectively.
INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name])
VALUES (1, 'Kelly', 'Jill')
You can omit the field list, but only if you supply all the values that record can contain.
INSERT INTO tblCustomers VALUES (1, Kelly, 'Jill', '555-1040',
'someone#microsoft.com')
Source MSDN How to: Insert, Update, and Delete Records From a Table Using Access SQL
The problem I see may be because of malformed SQL Statement. The string values( NVARCHAR, VARCHAR) should be enclosed within single quotes which I believe is not how you're doing now with following statement
string sqlinsert = "INSERT INTO Student VALUES (" + tBStudentNum.Text + "," + tBStudentName.Text+","+ tBCellNo.Text+","+ code + ")";
Try changing the SQL Statement to
string sqlinsert = $"INSERT INTO Student VALUES ({tBStudentNum.Text}, '{tBStudentName.Text}', {tBCellNo.Text}, '{code}')";
I've made an assumption in above case that tBStudentNum.Text and tBCellNo.Text are numeric values. If not, you can make appropriate changes to put the values inside single quote.
If you're using lower version of .net/C#, replace the $ expression with string.format function.
A number of observations:
You haven't specified the parameters in the SQL so we can only assume that there are four fields in the Student table.
You are not using named parameters - this is generally poor practice.
You are using concatenated values and SQL - this will leave you vulnerable to a SQL Injection attack
Any one of the text boxes might include a comma or other SQL formatting characters leading to SQL errors.
try
{
string Query = "SELECT Registrations list FROM [Records] WHERE textBox = '" + comboBox.SelectedValue + "'";
OleDbConnection me = new OleDbConnection(connection);
OleDbCommand constr = new OleDbCommand(Query, me);
OleDbDataReader reader;
connection.Open();
reader = constr.ExecuteReader();
if (reader.Read())
{
OleDbParameter parameter = constr.Parameters.Add(new OleDbParameter("Registrations list", OleDbType.Integer));
textBox.Text = reader["Registrations list"].ToString();
}
me.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Im trying to get database values to display in textbox but keep getting the error, i've tried mostly everything possible
wrap the column name with square brackets
SELECT [Registrations list] FROM [Records] WHERE textBox
Otherwise sql server looks for a column called Registrations and then tries to alias it as [List]
Enclose the column name in square brackets.
SELECT [Registrations list]
If the column names contais space then you need to enclose the column name in square brackets else SQL Server will consider it as two column names and since comma will also be not present hence it will give you syntax error.
I suppose there is an error in the SQL
string Query = "SELECT Registrations list FROM [Records] WHERE textBox = '" + comboBox.SelectedValue + "'";
Between SELECT and FROM there should be a comma separated list of columns belinging to the table Records. If you want to label the column place the keyword as between the column name and uts label.
If you placed a white space in the column name (never saw, never did, don't even know if it's possible at all), try including the column name between single quotes. Or (much better) rename the column.
I'm attempting to programmatically create a SQL table. I can create a table with a query, this is no issue at all. But I'd like the table name to have some relevance to the data inserted into it, as it's being used for quotations and invoices. Data entered from a DataGridView will be inserted into it (probably via bulkcopy, or something similar).
using (SqlCeCommand command = new SqlCeCommand(
"CREATE TABLE table1' (Weight INT, Name NVARCHAR, Breed NVARCHAR)", con))
works perfectly. However I'd like this code to work:
using (SqlCeConnection con = new SqlCeConnection(#"Data Source=|DataDirectory|\LWADataBase.sdf"))
{
con.Open();
try
{
string tableName = "" + quotenameTxt.Text + "-" +firstTxt.Text+ "-" + surenameTxt.Text;
using (SqlCeCommand command = new SqlCeCommand(
"CREATE TABLE '"+tableName.ToString()+"' (Weight INT, Name NVARCHAR, Breed NVARCHAR)", con))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Any suggestions? I get an error (as expected) but are unsure what I need to do.
I'm using SqlCe (and before anyone says "BulkCopy isn't supported", I know, I've got a reference that allows it)
The error I get is :
There was an error parsing the query. [ Token line number = 1,Token line offset = 16,Token in error = 1-2-3 ]
// "1-2-3" being the textbox values.
Change the dashes to underscores or surround the entire table name with [square brackets]
As was mentioned in comments above, make the following changes:
using (SqlCeCommand command = new SqlCeCommand(
"CREATE TABLE '"+tableName+"' (Weight INT, Name NVARCHAR, Breed NVARCHAR)", con))
tableName is already a string. No need to use .ToString() on it.
Also, you have a leading white space in your declaration of tableName:
string tableName = "" + quotenameTxt.Text + "-" + firstTxt.Text + "-"
+ surenameTxt.Text;
This makes the string " 1-2-3", not the "1-2-3" you are expecting.
Lastly, surround your tableName with [] to get it to work correctly:
using (SqlCeCommand command = new SqlCeCommand(
"CREATE TABLE '[" + tableName + "]' (Weight INT, Name NVARCHAR, Breed NVARCHAR)", con))
I want to add multiple values to a table cell separated by commas, the table I got is
Id ,Name , Type . I want to add multiple names in the name column, so the row will be something like:
ID Name Type
1 Peter, Jas , Roden , Karen Class A
I have done simple insertion which is:
[WebMethod]
public static string Insertion(string Name)
{
//List<string> result = new List<string>();
SqlConnection con = new SqlConnection("Data Source=XXX;Initial Catalog=XXX;User ID=sa;Password=XXXXX");
{
string query = "Insert into TestTable values #Name";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("#Name", SqlDbType.NVarChar).Value = Name;
{
con.Open();
cmd.ExecuteNonQuery();
//test();
con.Close();
return "True";
}
}
}
Can any one guide me on how to add multiple names which should be separated by commas, also I need to make sure there is no duplication of names.
you can send parameter value as "Peter, Jas , Roden , Karen" then it will insert that text in to given record.
but if you have array or List of names to be added, you can easily create the insert string as below
var names = string.Join("," ,namesArray.Distinct());
now you can call the service method using above generated names string
Can any one guide me on how to add multiple names which should be separated by commas
You need to use parameterized queries. It will also help you inb avoiding sql injection attacks.
like
cmd.Parameters.AddWithValue("#name",name);
also i need to make sure there is no duplication of names
Something like this should work for you
string name = "a,b,c,d,a,b,c,d";
HashSet<string> h = new HashSet<string>(name.Split(','));
string distinctNames = string.Join(",", h);