Insert to database Change the alias to a valid name? - c#

I have a sql table which has an image cloumn I'm trying to insert the image after converting it into byte.... the my insert query looks like this
INSERT INTO Member (F_NAME, L_NAME, D_O_B, UAE_ID_NO, MOBILE_NO, EMAIL_ID, REFERER, REF_CONTACT, ADDRESS, PICTURE) VALUES ('" + fname + "','" + lname + "','" + dob.ToShortDateString() + "','" + uaeid + "','" + mobile + "','" + emailid + "','" + reffere + "','" + refercontact + "','"+address+"',"+photo+")"
here pic is the image column... I'm converting the image into byte array using the following method
MemoryStream ms = new MemoryStream();
//save the image into memory stream
pBoxMember.Image.Save(ms, ImageFormat.Jpeg);
//assign the byte array with total size of memorystream
photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, photo.Length);
return true;
But it is giving me this error
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
invalid syntax near ".

If this should work at all this should look like this:
var sql = "INSERT INTO Member (F_NAME, L_NAME, D_O_B, UAE_ID_NO, MOBILE_NO, " +
"EMAIL_ID, REFERER, REF_CONTACT, ADDRESS, PICTURE) " +
"VALUES ('" + fname + "','" + lname + "','" + dob.ToShortDateString() + "','" + uaeid + "','" + mobile + "','" + emailid + "','" + reffere + "','" + refercontact + "','" + address + "'," + photo + ")"
But you should use SQL Params for this, something like:
string queryString = "INSERT INTO Member (F_NAME, L_NAME, D_O_B, UAE_ID_NO, ... VALUES(#fname ,#lname ....)";
SqlCommand cmd = new SqlCommand(queryString, dbConnection);
cmd.Parameters.AddWithValue("#fname",fname);
cmd.Parameters.AddWithValue("#lname",lname);
...
cmd.Parameters.AddWithValue("#blobParam",YourBytesArray);
...
cmd.ExecuteNonQuery();

Related

Exception Inserting Image To MSAccess Database

Here I am inserting an image in msaccess database (accdb). I am not able to figure out why this is generating expcetion. It says Error in insert into statement.
String q = #"Insert Into tblModal (ModalName, CategoryId, Gender, Type, Description, image, LastUpdated) values ('" +
txtModalName.Text + "','" + categoryId + "','" + gender + "','"+txtType.Text +"', '" + txtDescription.Text + "',#pic, '" + DateTime.Now.ToString() + "')";
OleDbCommand cmd = new OleDbCommand(q);
cmd.Parameters.AddWithValue("#pic", Check.imageToByteArray(pictureBoxPhoto.Image));
int res;
res = br.ExecuteNonQuery(cmd);

C# insert into Access database

I have a simple user interface for an inventory database. The operation will be insert into, edit existing, view data grid, etc....There are a total of 4 fields for the inventory. The insert statement I am using works but if one of the entries does not have a value then it shows an error when trying to insert.
" Data type mismatch in criteria expression "
command.CommandText = "insert into Inventory(SerialNumber,PartNumber,ROnumber,Location)
values ('" + txtPart.Text + "','" + txtSerial.Text + "','" +
txtRO.Text + "','" + txtLocation.Text + "')";
I assume it is because the code needs a value for each field so how do I get around this issue?.
Actually when you trying to use this query you have said the First Parameter is SerialNumber and then PartNumber and when you are passing its reverse.
command.CommandText = "insert into Inventory(SerialNumber,PartNumber,ROnumber,Location)
values ('" + txtPart.Text + "','" +
txtSerial.Text + "','" +
txtRO.Text + "','" +
txtLocation.Text + "')";
Due to this reason the fields you are entering have different size May be part number is bigger in size instead of SerialNumber or vice versa. So you should change it to
command.CommandText = "insert into Inventory(SerialNumber,PartNumber,ROnumber,Location)
values ('" + txtSerial.Text + "','" +
txtPart.Text + "','" +
txtRO.Text + "','" +
txtLocation.Text + "')";

MS Access DB Insert Query Not Working

I am trying to insert Data into MS ACCESS DB. Everything is fine. Connection, DB Path etc.
There is a table CIT in it.
I am using this Insert into query
string query = "INSERT INTO CIT (GRNO:, Name, FName, CNIC, Address, ContactNO, Gender, Qualification, DOB, RegDate, Photo) VALUES ('" + txtGRNO.Text + "','" + txtName.Text + "','" + txtFName.Text + "','1234','" + txtAddress.Text + "','" + txtContact.Text + "','" + cBoxGender.Text + "','" + cBoxQual.Text + "','" + dteDOB.Text + "','" + dteReg.Text + "','" + path + "');";
I tried everything but cant seem to find what is wrong here. The datatype of fields is Text in DB, & when I execute the query , it gives the error
Your table includes 2 fields whose names are problematic: GRNO:; and Name.
Since GRNO: includes a colon, you can enclose it in square brackets so the db engine will accept it: [GRNO:]
And since Name is a reserved word, enclose that one in square brackets, too.
"INSERT INTO CIT ([GRNO:], [Name], ...
Beyond those field name issues, the standard advice is to use a parameter query for your INSERT. Note you will still need to bracket those problem names in a parameter query.
Also, Access will let you use back-ticks instead of square brackets if you prefer ...
"INSERT INTO CIT (`GRNO:`, `Name`, ...
Try:
string query = "INSERT INTO CIT (GRNO, Name, FName, CNIC, Address, ContactNO, Gender, Qualification, DOB, RegDate, Photo) VALUES ('" + txtGRNO.Text + "','" + txtName.Text + "','" + txtFName.Text + "','1234','" + txtAddress.Text + "','" + txtContact.Text + "','" + cBoxGender.Text + "','" + cBoxQual.Text + "','" + dteDOB.Text + "','" + dteReg.Text + "','" + path + "');";
Remove ; after GRNO field.

error when i insert data and select last id

I want to get id after I insert a row of data into a table but I get an error:
System.InvalidCastException: Specified cast is not valid.
This is my code
SqlCommand cmd_insert = new SqlCommand("insert into ITM_OrderItem (ITM_Code, ITM_Desc, ORCAT_Desc, ORSCT_Desc, ARCBG_Abbrev, ARCSG_Desc, ARCSC_Code, ARCIM_DerivedFeeItem, OPD_Price, IPD_Price, Tou_Price, App_Status, SITES, CSSUSR_RowId, CSSUSR_DepId, CSSUSR_PosId , CDate, MDate) values('"
+ dt.Rows[i]["EARCIM_Code"].ToString() + "','"
+ dt.Rows[i]["EARCIM_Desc"].ToString() + "','"
+ dt.Rows[i]["EORCAT_Desc"].ToString() + "','"
+ dt.Rows[i]["EARCIC_Desc"].ToString() + "','"
+ dt.Rows[i]["EARCBG_Abbrev"].ToString() + "','"
+ dt.Rows[i]["EARCSG_Desc"].ToString() + "','"
+ dt.Rows[i]["EARCSC_Desc"].ToString() + "','"
+ dt.Rows[i]["EARCIM_DerivedFeeItem"].ToString() + "','"
+ dt.Rows[i]["OPD"].ToString() + "','"
+ dt.Rows[i]["IPD"].ToString() + "','"
+ dt.Rows[i]["Tourist"].ToString() + "','"
+ status_app + "','"
+ usr_site + "','"
+ usrID + "','"
+ Dep_RowId + "','"
+ Pos_RowId + "','"
+ Cdate + "','"
+ Cdate + "') select SCOPE_IDENTITY()", conEMR);
// error on this line
insert_RowId = (Int32)cmd_insert.ExecuteScalar();
Thank you a lot.
It looks as if the identity column in your table ITM_OrderItem does not use int as data type, What I can guess is that it is a bigint data type and if that is the case you should change the line in your code to:
insert_RowId = (Int64)cmd_insert.ExecuteScalar();
Also make sure that you change the datatype of variable 'insert_RowId' to Int64 in your C# code declaration.

exception with nvarchar - data was truncated while converting from one data type to another

I'm having a bit of trouble with entering data into several columns of a database, all nvarchar types. I'm getting:
The data was truncated while converting from one data type to another. Name of function(if known)
DBConn.Open();
cmd = new SqlCeCommand("INSERT INTO [Employee Table] VALUES ('"+ social + "','" +
first + "','" + last + "','" + mid + "','" + address + "','" + phone + "',"
+ "'Employee'" + ",'" + city + "','" + state + "','" + zip + "','" + email + "','" + userName + "')", DBConn);
cmd.ExecuteNonQuery();
Any suggestions on how to avoid the truncating/converting error?
Your NVARCHAR isn't big enough to hold your string.
try using Parameters
SqlParameter param = new SqlParameter("Field", SqlDbType.NVarChar)
Param.Value = Field;
The reason is that the column's length is very short. So the value is longer than the maximum width of the column of the table.

Categories