I get the error that ID cant be Null, but it should auto increment
Using this code:
cn.Open();
cmd.CommandText = " insert into Firmen(Firmenname,UstID,Telefonnummer) Values('" + tbFName.Text + "','" + tbUstID.Text + "','" + tbTelF.Text + "')";
cmd.ExecuteNonQuery();
cn.Close();
I thought it would auto increment when I just Ignore the ID. Please help me
Related
I'm new in this field. Trying to insert the values from textbox to my database table, but I get an error at
adapter.InsertCommand.ExecuteNonQuery();
Can anyone help me solve this?
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";
command = new SqlCommand(sql,con);
adapter.InsertCommand = new SqlCommand(sql,con);
// this line here is showing the error
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
con.Close();
Since your table is called table and that is a SQL reserved word, you have two choices:
Change your table name. This is the only option you should be considering but for completeness;
Quote the name of the table:
insert into [table] values....
You do not list your column name on insert. This means you are also attempting to insert your identity column as well. Always list your column names
insert into NewName (firstname, lastname, username, email, password, contact)
values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')
Yes I've done it .I was using "user" in table column which is not allowed .After changing the column name everything works.
This is the code
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";
command = new SqlCommand(sql, con);
adapter.InsertCommand = new SqlCommand(sql, con);
// this line here is showing the error
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
con.Close();
I think its one of those question where you need a second pair of eyes to have a look at.
I trying to add new record and just keep getting same error which is "Error in SQL syntax , I am using MySQL and here is the table
script for MySQL
create table tbl_employee(
employeeID smallint,
employee_Fname varchar(30),
employee_Sname varchar(30),
employee_AddressL1 varchar(100),
employee_AddressL2 varchar(100),
employee_PostCode varchar(10),
employee_tel_type enum('work', 'mobile', 'personal'),
employee_Image varchar(250),
employee_Job_Role enum('admin','accounts','management','maintiantance','Sales'),
employee_Salary float,
employee_imagePath varchar(250),
employee_tel_no varchar(100),
Primary key(employeeID)
);
and here is the C# code
connected = DataConnection.getConn();
MySqlCommand cmd = new MySqlCommand("", connected); //
//This is my insert query in which i am taking input from the user through windows forms
//insert into tbl_employee value (2,'John','Rafiq','234 Zoo Rd','Alcala','2388','work' ,'admin',3500.89,'C:\blindXimages','111-111' );
string Query = "Insert into tbl_employee (employeeID,employee_fname,employee_Sname,employee_AddressL1,employee_AddressL2,employee_PostCode, employee_tel_type,employee_Job_Role,employee_Salary,employee_ImagePath,employee_tel_no)" +
"'value('" + Convert.ToInt32( this.txtEmployeeID.Text) + "'," + this.txtName.Text + "','" + this.txtSurname.Text + "','" + this.txtAddressL1.Text + "','" + this.txtAddressL2.Text + "','" + this.txtPostCode.Text + "','" + this.txtTelType.Text + "','" + this.txtJobRole.Text + "','" + Convert.ToDecimal( this.txtSalary.Text) + "','" + this.txtFaceName.Text + "','" + this.txtTelephoneNo.Text + "')";
//This is command class which will handle the query and connection object.
MySqlCommand MyCommand2 = new MySqlCommand(Query, connected);
MySqlDataReader MyReader2;
MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database.
MessageBox.Show("Save Data");
while (MyReader2.Read())
{
}
// connected.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
btnAddNew.Enabled = false;
MessageBox.Show("Data is save clearing all text boxes ");
clearTextBox();
}
I have double and triple check my syntax just can't figure out were I am going wrong line by line. will very much appropriate this support.
use "values" instead of "value" and add space before values.
I am creating a window application and I've chosen a database from the New Item menu. My insert query below is not executing:
con.Open();
cmd = new SqlCommand("insert into record values('" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','"+textBox13.Text+"','"+textBox12.Text+"')", con);
cmd.ExecuteNonQuery();
con.Close();
In Sql when your Insert query does not contain the column names then the values have to be in the correct order. Maybe this is why it is failing. You did not provide us with an error so i dont know.
You are trying this:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
I am suggesting to try this:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
If you can you should use parameters and include your column names (like how Andreas suggested) . An example would be...
cmd = new SqlCommand("INSERT INTO record (column1, column2, column3,...)
VALUES (#data1, #data2, #data3,...)", con);
cmd.Parameters.AddWithValue("#data1", textbox2.Text);
cmd.Parameters.AddWithValue("#data2", textbox3.Text);
cmd.Parameters.AddWithValue("#data3", textbox4.Text);
cmd.Parameters.AddWithValue("...", ...);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
I am getting an error while i am trying to insert a user id of my client in the ms access database.
The error i am getting is Overflow.
when i am trying to insert its getting the above error.
I am using these code.
OleDbCommand cmd = new OleDbCommand("insert into UserInfo " + "([firstname], [lastname], [gender], [occupation], [expirydate], [UserId], [phoneno]) " + " values('" + txt_FirstName.Text + "','" + txt_LastName.Text + "','" + cmb_Gender.Text + "','" + cmb_Occupation.Text + "','" + txt_expiryDate.Text + "','" + txt_HardDiskId.Text + "','" + txt_PhoneNo.Text + "');", con);
OleDbCommand cmd1 = new OleDbCommand("select * from UserInfo where (HardDiskId='" + txt_HardDiskId.Text + "')", con);
int temp = 0;
try
{
con.Open();
string count = (string)cmd1.ExecuteScalar();
if ((count == "") || (count == null))
{
temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("User ID of " + txt_FirstName.Text + " " + txt_LastName.Text + " has been added");
}
else
{
MessageBox.Show("Record not added");
}
}
else
{
MessageBox.Show("User ID of " + txt_FirstName.Text + " already exists. Try another user ID.");
}
}
Aside from being open to SQL Injection (which you should parameterize your OleDbCommand), first thought on a problem is what you are trying to store the data. Do any of the text fields have special characters or apostrophe in name which would otherwise pre-terminate your embedded .... '" + nextField + "' ..." entries and throw the balance off.
Another... don't know if the parser is picky or not... but a space after values, before open paren.... " values(" to " values (".
Third, and more probable the issue is the expiration date. If its a date field, and you are trying to put in as text, it might be failing on the data type conversion.
I am using the following command for inserting a record in an MS Access database:
OleDbCommand cmd = new OleDbCommand("insert into Table1 values('" + Name + "','" +
Symbol + "','" + D + "','" + Red + "','" + RedBuy + "','" + RedSell + "','" +
SBIntraBuy + "','" + SBTR1Buy + "','" + SBTR2Buy + "','" + SBTR3Buy + "','" +
SBIntraSell + "','" + SBTR1Sell + "','" + SBTR2Sell + "','" + SBTR3Sell + "','" + RSTL
+ "','" + Green + "');", con);
I want to add a column before Name called CustomerId with a Primary key. I don't know how to insert it with primary key. What i should add in my command for primary key?
There would be a great appreciation if someone could help me.
Thanks In Advance.
Check this link for screen shot of the table layout.
If your key is auto increment, then you don't need to. The database will do it for you. If your key is not auto increment, consider changing it. It's really neat!
Also, please consider using binding instead of building a full insert query.
Suppose the following table:
Table1
PK (auto_increment)
Name
Symbol
What you will want to do is:
OleDbCommand cmd = new OleDbCommand("insert into Table1 (Name,Symbol) values (?,?)");
cmd.Parameters.Add(new OleDbParameter("#Name",Name));
cmd.Parameters.Add(new OleDbParameter("#Symbol",Symbol));
or (safer):
OleDbCommand cmd = new OleDbCommand("insert into Table1 (Name,Symbol) values (#Name,#Symbol)");
cmd.Parameters.Add(new OleDbParameter("#Name",Name));
cmd.Parameters.Add(new OleDbParameter("#Symbol",Symbol));
And finally:
cmd.ExecuteNonQuery();