Format of date inserting wrong on database - c#

This is my code for the date:
DateTime datetry = DateTime.Now;
lebeldet.Text = string.Format("{0:MMM/dd/yyyy h:mm tt}", datetry);
In label, its displaying the result that i wanted
:
But when its inserted into the database and crystal report print it looked like this.
this is my code for insertion.
crud.AddRecord("Update SalesTransaction Set Date = '" + Convert.ToDateTime(lebeldet.Text) + "' , Cashier = '" + cashier.Text + "', TotalSales = '" + totaldue.Text + "' , SubTotal = '" + subtotal.Text + "' , Discount = '" + discount.Text + "' , DiscountableSales = '" + totaldiscountablesales.Text + "' , Vat = '" + totalvat.Text + "' , VatableSales = '" + totalvatablesales.Text + "' where TransactionID = '" + salestransaction.TransactionID+"' ");
How can i make the date to be inserted like "Aug/03/2017 12:30AM" ?

But when its inserted into the database and crystal report print it looked like...
A datetime value in a database, or a DateTime value in C#, do not have any format. They represent a value. You'll see a string in a particular format when you evaluate it, such as with the ToString method.
In a presentation tool, such as when you view the string in a table in SQL Management Studio, or in the Visual Studio debugger, there is an implicit call to ToString (or similar) occurring such that you can see the data. The format of that is specific to the tool being used. It is NOT part of the data itself.
For Crystal Reports and other reporting tools, you'll usually find a separate field or function to control the format displayed in the report. You do this on the field in the report design, not on the data in the database.

Related

C# query not translating to MYSQL

String query6 = "
SELECT p.*,SUM(L.qty)as sales
FROM product p,purchaseLog L
WHERE L.purchaseDate
BETWEEN '"+ startDate.Value.Date.ToString("yyyy-dd-MM") + "'
AND '"+endDate.Value.Date.ToString("yyyy-dd-MM") +"'
AND p.id=l.itemID GROUP BY p.product_name
ORDER BY sales ASC LIMIT " + " " + " " + count + "";
This is the query that I made in Visual Studio. I tried inputting this query manually without variables in phpmyadmin and it works just fine. But for some reason when I write the query down and pass it to a mysqlcommand and mysqldatareader it doesn't detect the date. The count used not work too. But adding space in between the word "limit" and the count made it work.
Is C# trimming some parts of my queries?
This: 'yyyy-dd-MM' is the wrong format. It must be "yyyy-MM-dd". Ideally you'd even prefix this with the word DATE so as to mark the string as a date literal:
" ... WHERE L.purchaseDate BETWEEN DATE '" + startDate.Value.Date.ToString("yyyy-MM-dd") +
"' AND DATE '" + endDate.Value.Date.ToString("yyyy-MM-dd") + "'... "

programming for keyword search from database?

Hello I have a table with following columns.
Profile_ID,
Name,
Age,
More_Info_about family,
qualification details,
job details and
partner_preference
I have to search on keyword basis and keyword is to be taken from textbox.
The search result should be in such a way so that if keyword presents in starting, ending and in between the sentence of any of the columns presents in where conditions.
I tried LIKE '" + txtbox_keyword.Text + "'
but is not working properly it is not searching data if keyowrd is present in between sentense.
select (Profile_ID,Name,Age, More_info_about_family,Job_Business_Location_City,Salary
from tblRegistration
WHERE More_info_about_family LIKE '" + txtbox_keyword.Text + "'
OR LIKE '" + txtbox_keyword.Text + "'
OR origin LIKE '" + txtbox_keyword.Text + "'
OR Job_Detail LIKE '" + txtbox_keyword.Text + "' ", con);
Use sqlParameter for except sql injection and you need add %% in your query to search by field.
SqlParameter parameter = new SqlParameter("#keyWord",txtbox_keyword.Text);
select (Profile_ID,Name,Age, More_info_about_family,Job_Business_Location_City,Salary
from tblRegistration
WHERE More_info_about_family LIKE '%#keyWord%'
OR LIKE '%#keyWord%'
OR origin LIKE '%#keyWord%'
OR Job_Detail LIKE '%#keyWord%', con,parameter);

Adding fields in in c# to crystal report

i want to display a set of report on my crystal report form in a app i develop using win form, c# and crystal report. i have 2 tables and wants to display a report from the two tables in one crystal report. here is the code
string sql = "select * from ISSUED, RECEIVED WHERE ISSUED.ITEMCODE=RECEIVED.ITEMCODE AND ISSUED.TRANSDATE,RECEIVED.TRANSDATE between'" + Convert.ToDateTime(date1) + "' and '" + Convert.ToDateTime(date2) + "' and ISSUED.ITEMCODE = '" + itemcode + "' and RECEIVED.ITEMCODE between '" + Convert.ToDateTime(date1) + "' and '" + Convert.ToDateTime(date2) + "'";
And this is the error i get
An expression of non-boolean type specified in a context where a condition is exprected, near '.'.

Converting a string into the Date datatype and inserting into the database

I have a rent_date and return_date column in rental table and the data-type is Date. I have created two different strings for them. rentDate is the current date and returnDate is the output of adding the days that the product is rented for, from the current date. I tried to convert the strings using to_date but its showing errors. I have tried it with the data-type varchar2(30). Worked fine. But had to modify to Date because I want a late return date too. Can anyone tell me what is wrong with this query?
DateTime dt = System.DateTime.Now;
string rentDate = dt.ToString();
// this is added to the current date of the user input and showed in the form
string returnDate = label66.Text;
string Query = "Insert into rental ( pr_ID, cid, rent_date, Return_date, status, receipt_no) values ( '" + comboBox5.Text + "' , '" + comboBox4.Text + "', (to_date('" + rentDate + "','dd/mm/yyyy')), (to_date('" + returnDate + "','dd/mm/yyyy')), '" + Status + "', '" + txt_recpt.Text + "') ";
What error are you getting?
I agree with the "Soner" that they should not be text values. However, based on your existing code, you may be getting a format error.
What format is the string value of "rentDate" and "returnDate" in? It needs to be the same as what you've specified in the INSERT query, which is "dd/mm/yyyy". If it's not, an error will be thrown.
The second parameter of the TO_DATE function is the format of the input variable.
Unless you're getting a different error?

Question about adding to database from text entered into textbox

I have an app that displays data from a sql database in a datagrid. I am adding a feature that will allow a user to add a new item to this datagrid. I will have an Add button in the app that when clicked, will open a new window where all the info will be added. After the user inputs the info into the text boxes and clicks save, that info gets saved to the database. This will require inserting data into two separate tables that are related. The first table is ItemDescriptor and the other table is Accessories. The id for ItemDescriptor has to be put into the Accessories table as ItemDescriptorID. I have tried to do two separate insert queries but cant figure out how to get the id from ItemDescriptor into the Accessories table programatically. Sorry, I know this might be a little confusing. Let me know if more info is needed. Thanks
_dbAccess.ExecuteNonQuery("INSERT INTO ItemDescriptor (id, Model, Brand, DeviceName, Type, Notes) VALUES ('" + System.Guid.NewGuid() + "', '" + tbModel.Text + "', '" + tbBrand.Text + "', '" + tbDeviceName.Text + '", '" + cmbType.SelectedValue + "', '" + tbNotes.Text + "')");
_dbAccess.ExecuteNonQuery("INSERT INTO Accessories (id, ItemDescriptorID, StorageRoomCount, OnHandCount, HiBayCount) VALUES ('" + System.Guid.NewGuid() + "', '" +
and thats about as far as i got...not sure how to get the ItemDescriptorID in there from the first query.
First, please please please use parameterized queries when dealing with user-supplied input. That string concatenation stuff is just asking for a SQL injection attack.
Second of all, if you're using SQL Server, you can use something like ##IDENTITY to get the identity field after insertion. There's something comparable on all systems, so if you let us know what you're using we can point you in the right direction.
I'm assuming that you're using SQL Server and that ItemDescriptor.id is an identity column; correct?
If you execute the following command immediately after your initial insert statement, it will return the value of the most recently generated identity:
var newID = _dbAccess.ExecuteScalar("SELECT Scope_Identity()");
You can use newID to construct your second insert statement.
One caveat: The SELECT Scope_Identity() statement must be executed on the same connection instance as the initial insert statement. So ensure that you're not closing and reopening your connection between statements. If this seems to be a problem, you can actually combine your insert and select statements into a single command; e.g.:
var newID = _dbAccess.ExecuteScalar("INSERT [all your code here]; SELECT Scope_Identity();");
Good luck!
Check out this site about Getting the identity of the most recently added record
I figured out a way to make it work. I just made the guid an object and then used it as a variable and that worked. thanks for all the suggestions.
Guid guid = System.Guid.NewGuid();
if (_dbaccess != null)
{
_dbAccess.ExecuteNonQuery("INSERT INTO ItemDescriptor (id, Model, Brand, DeviceName, Type, Notes) VALUES ('" + guid + "', '" + tbModel.Text + "', '" + tbBrand.Text + "', '" + tbDeviceName.Text + '", '" + cmbType.SelectedValue + "', '" + tbNotes.Text + "')");
_dbAccess.ExecuteNonQuery("INSERT INTO Accessories (id, ItemDescriptorID, StorageRoomCount, OnHandCount, HiBayCount) VALUES ('" + System.Guid.NewGuid() + "', '" + guid + "', '" + tbStorageRoom.Text + "', '" + tbOnHand.Text + "', '" + tbHiBay.Text + "')");
}

Categories

Resources