For my code I INSERT a row and I would like to UPDATE that row.
cmdRstCmdN.CommandText = "INSERT INTO Login_Details (userName,password , role,login_start) VALUES ('" + userName + "','" + password + "','" + role + "','" + login_start + "')";
cmdRstCmdN.ExecuteNonQuery();
For above I INSERT userName, password, role, login_start INTO Login_Details. So now I would like to UPDATE this row with login_end My code is below, but it's not work. Could you help me to fix this code. I think I may wrong on some syntax or somethings.
cmdRstCmdN.CommandText = "UPDATE Login_Details SET login_end ='" + login_end + "' WHERE login='" + countRow + "'";
cmdRstCmdN.ExecuteNonQuery();
Thanks all.
Related
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 + "')";
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.
I've searched google and youtube and here of course and i can't find the answer. I need to turn the identity insert On and OFF but don't know how to do that in the sql command of the ADO.Net project. I'm sure it must be extremely easy for you highly experienced people.
additional information I'm using c#, visual studio and sql server, i don't know if that helps narrow the problem down?
here is sql command i tried in the ado.net project.
sql = "SET IDENTITY_INSERT HomeCareVisit ON insert into HomeCareVisit values("
+ VisitRefNo + ",'" + PatientNo + "','" + ScheduledDateTime + "','"
+ TreatmentInstructions + "','" + MedicalStaffID + "','" + Priority + "','"
+ ActualVisitDateTime + "','" + TreatmentProvided + "','" + Prescription
+ "','" + AdvisoryNotes + "'," + (FurtherVisitRequired ? "1" : "0") + " );";
and it gave the error
Violation of PRIMARY KEY constraint 'PK_VisitRefNo'. Cannot insert duplicate key in object 'dbo.HomeCareVisit'.
The statement has been terminated.
here is the code shows what data type the column are
VisitRefNo = ds.Tables[0].Rows[i].ItemArray[0].ToString();
PatientNo = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[1]);
ScheduledDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[2]);
TreatmentInstructions = ds.Tables[0].Rows[i].ItemArray[3].ToString();
MedicalStaffID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[4]);
Priority = ds.Tables[0].Rows[i].ItemArray[5].ToString();
ActualVisitDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[6]);
TreatmentProvided = ds.Tables[0].Rows[i].ItemArray[7].ToString();
Prescription = ds.Tables[0].Rows[i].ItemArray[8].ToString();
AdvisoryNotes = ds.Tables[0].Rows[i].ItemArray[9].ToString();
FurtherVisitRequired = Convert.ToBoolean(ds.Tables[0].Rows[i].ItemArray[10]);
This is the command to do it
SET IDENTITY_INSERT YourTable ON
Try this:
sql = "SET IDENTITY_INSERT HomeCareVisit ON insert into HomeCareVisit(column_names) values("
+ VisitRefNo + ",'" + PatientNo + "','" + ScheduledDateTime + "','"
+ TreatmentInstructions + "','" + MedicalStaffID + "','" + Priority + "','"
+ ActualVisitDateTime + "','" + TreatmentProvided + "','" + Prescription
+ "','" + AdvisoryNotes + "'," + FurtherVisitRequired + " );";
where column_names is list of table columns in order in which you insert.
I think you pass string in place where table doesn't expect this, like TreatmentProvided column (by name, i assume that you pass True/False, but not as string).
i have another question for anyone willing to help. This should be fairly easy but for whatever reason i cant seem to get it right. Im working with visual studio 08 creating a asp.net website in c#
im trying to have a user register, which is all working great, and have the new user default as a user and not a admin. I am using a .MDB as where everything is stored.
is it possible to do this in my initial sql query?
currently my code looks like
strSQL = "Insert into tblUserLogin " +
"(UserFirstName, UserLastName, UserName, UserPassword, UserAddress, UserCity, UserState, UserZipCode, UserEmail, UserPhone, ) values ('" +
UserFirstName + "', '" + UserLastName + "', '" + UserName + "', '" + UserPassword + "', '" + UserAddress +
"', '" + UserCity + "', '" + UserState + "', '" + UserZipCode + "', '" + UserEmail + "', '" + UserPhone +
"')";
// set the command text of the command object
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// Execute the insert statement
command.ExecuteNonQuery();
this will successfully post everything to the .MDB and it will leave the cell under the column SecurityLevel empty.
i attempted to add
"(UserFirstName, UserLastName, UserName, UserPassword, UserAddress, UserCity, UserState, UserZipCode, UserEmail, UserPhone, SecurityLevel=U )
but this did not work as i hoped it would. Is there a simple way to have that value SecurityLevel default to U without anyone having to specify it?
thank you for your time
If I understand you correctly just use a const value 'U' for SecurityLevel column
strSQL = "INSERT INTO tblUserLogin " +
"(UserFirstName, UserLastName, " +
" UserName, UserPassword, " +
" UserAddress, UserCity, " +
" UserState, UserZipCode, " +
" UserEmail, UserPhone, SecurityLevel) " +
"VALUES (" +
UserFirstName + "', '" + UserLastName + "', '" +
UserName + "', '" + UserPassword + "', '" +
UserAddress + "', '" + UserCity + "', '" +
UserState + "', '" + UserZipCode + "', '" +
UserEmail + "', '" + UserPhone + "', 'U')"; // pass a const value for SecurityLevel column
// the rest of your code goes here
On a side note: Your code is vulnerable to sql-injections. Learn and use prepared (parameterized) statements instead of dynamically building a query strings.
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();