Adding fields in in c# to crystal report - c#

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 '.'.

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);

Format of date inserting wrong on database

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.

Incorrect string value(C# Visual Studio 2015, MySQL Server 5.5(Console))

I need help! I write software for database management for the course. I can not complete the transaction by inserting data. I am from Ukraine and use Ukrainian data on database, but the transaction is not completed bringing the error "Incorrect string value: '\ xD0 \ xB2' for column 'User_name' at row 1 "} System.Exception {MySql.Data .MySqlClient.MySqlException}
" I read all the articles on the stack overflow but nothing helped me(
i use SET NAMES 'utf8'(cp1251, utf8mb4, koi8r, win1251, cp866 and other) but nothing work(help, the problem may be encoded on the development environment?
i use MySql.Data.MySqlClient or MySQL.dll
connect code ->
connStr = "server= localhost;user=root;charset=utf8mb4;database=DB_Journal;port=3306;password=masterkey;"conn = new MySqlConnection(connStr);
and insert ->
conn.Open();
string sql = "insert into Users(User_id, User_name, User_surname, User_fname, Login, UType_id, Password,Secret,Answer) values (null, '" +
textBox1.Text + "', '" + textBox2.Text + "', '"
+ textBox4.Text + "', '" + textBox3.Text + "', '"
+ usr + "', '" + pass + "', '"
+ richTextBox1.Text + "', '"
+ textBox7.Text + "')";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
(I'm sorry, I can write not correctly, but i study hard)
I don't know what you are trying to accomplish with SET NAMES, but it probably does not do what you think it does. (Besides, its documentation explicitly says that it won't work with utf8.)
It is kind of hard to tell without seeing your CREATE TABLE statement, but what is probably happening is that you have declared your User_name etc. columns as being of type CHAR or VARCHAR while in fact they should be of type NCHAR or NVARCHAR.

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