I have been struggling to find an answer to this. I am importing an Excel spreadsheet into C# using Oledb. This works fine, however while importing I wish to join two of the Excel columns together i.e concatenate them.
This is the code I currently have:
string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + textBox1.Text + ";Extended Properties=\"Excel 12.0 XML;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [ID], [Subject], [Catalog], [Last], [First Name], [Descr], [Mark] from[" + textBox2.Text + "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
The code above is working fine. The two columns I wish to join are Subject and Catalog and for this to be called Module. Subject is a string of three letters and Catalog four numbers. Is there a way to do this within the select statement or an alternative method?
Many thanks in advance.
Change your OleDbAdapter query like this:
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [ID], [Subject] + ' ' + [Catalog] AS [Module], [Last], [First Name], [Descr], [Mark] from[" + textBox2.Text + "$]", conn);
Notice that [Subject] + ' ' + [Catalog] AS [Module] are combined into a single string and placed under Module alias.
You can "CONCAT" in the query:
https://msdn.microsoft.com/es-es/library/hh231515(v=sql.120).aspx
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [ID], CONCAT( [Subject], [Catalog] ) AS subject_catalog, [Last], [First Name], [Descr], [Mark] from[" + textBox2.Text + "$]", conn);
Related
No idea how to fix that.
So i have an imported table (screenshot) Patient (# 1) and Diagnoz (Diagnosis) (# 2) from the MS SQL Server. The first table contains a complete list of patient data and a foreign key for the id_diagnoz in which the Stage and Name_diagnoz is indicated.
Table (# 3) is a ready-made option for outputting data without unnecessary columns, but this is not a problem."select Pacient.id_patient, Pacient.Name, Pacient.Surname, Pacient.Middle_name, Pacient.Age, Pacient.Legal_address_Clinic, Diagnoz.Name_diagnoz, Diagnoz.Stage FROM Pacient JOIN Diagnoz ON Diagnoz.id_diagnoz = Pacient.id_diagnoz"; That's work well.
The problem is that when filling in the data from the textboxes and comboboxes, I need to first generate an id_diagnoz from the table (# 2) and with it add the other info to the patient table (# 1). However, I could not manage to do this for several hours, because even the error did not lead. Therefore, I decided to separately add the id_diagnoz to the empty table (# 4) to make sure that my query is working.
Please, help me with this id_diagnoz and other data put into the patient table.
private void button5_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=DESKTOP-R551818\\SQLEXPRESS;Initial Catalog=Fond;Integrated Security=True"))
{
SqlDataAdapter comm = new SqlDataAdapter("SELECT id_diagnoz FROM Diagnoz WHERE Name_diagnoz = '" + comboBox2.Text + "' and Stage = '" + comboBox3.Text + "'" +
"INSERT INTO Pacient (Name, Surname, Middle_name, Column__Passport, Legal_address_Clinic, Age)" +
" VALUES (#Name, #Surname, #Middle, #Passport, #AddresClinic, #Age) ", conn);
comm.SelectCommand.Parameters.AddWithValue("#Name", textBox3.Text);
comm.SelectCommand.Parameters.AddWithValue("#Surname", textBox4.Text);
comm.SelectCommand.Parameters.AddWithValue("#Middle", textBox5.Text);
comm.SelectCommand.Parameters.AddWithValue("#Passport", maskedTextBox1.Text);
comm.SelectCommand.Parameters.AddWithValue("#AddresClinic", comboBox1.Text);
comm.SelectCommand.Parameters.AddWithValue("#Age", textBox7.Text);
DataSet ds = new DataSet();
comm.Fill(ds);
//da.Fill(ds, "Pacient");
dataGridView2.DataSource = ds.Tables[0];
conn.Close();
conn.Open();
}}
Screenshot for understanding
Shouldn't you specify id_diagnoz in your insert query and supply the value of that field with the subquery you had there?
Something like:
INSERT INTO Pacient (Name, id_diagnoz, Surname, Middle_name, Column__Passport, Legal_address_Clinic, Age)
VALUES (#Name,
SELECT id_diagnoz FROM Diagnoz WHERE Name_diagnoz = '" + comboBox2.Text + "' and Stage = '" + comboBox3.Text + "'",
#Surname, #Middle, #Passport, #AddresClinic, #Age)
My code is
string PathConn = "Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox10.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter("Select * From [" + textBox1.Text + "$]", conn);
DataTable dt = new DataTable();
dbDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
I get the error:
System.Data.OleDb.OleDbException: ''$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long
What am I doing wrong?
Could it possibly be the "$"
in
("Select * From [" + textBox1.Text + "$]", conn);
Try
("Select * From "+ textBox1.Text , conn);
or
("Select * From ["+ textBox1.Text +"]", conn);
which looks like a more correct sql statement to me.
And if I might make a suggestion, try giving your text boxes names that mean more so that your code is more maintainable in the future.
I want to get from my database two columns and to put it in my DropDownList but when I write this code the software give me error message:"Data Association: System.Data.Common.DataRecordInternal not contain a property named 'ProductManufacturer ProductModel'." and put the "ProductsList.DataBind();" in red color.
What is the problem and how can I show ProductManufacturer & ProductModel in the DropDownList.
OleDbConnection con11 = new OleDbConnection();
con11.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("") + "\\DataBases.accdb";
con11.Open();
string sql1 = "select * from ProductsTable";
OleDbCommand cmd1 = new OleDbCommand(sql1, con11);
OleDbDataReader Dr1 = cmd1.ExecuteReader();
ProductsList.DataSource =Dr1;
ProductsList.DataTextField = "ProductManufacturer" + " " + "ProductModel";
ProductsList.DataValueField = "ProductModel";
ProductsList.DataBind();
Combine them in your query.
string sql1 = "select *, ProductManufacturer + ' ' + ProductModel as someField from ProductsTable";
ProductsList.DataTextField = "someField";
Hello Everyone I got a little confuse about selecting multiple fields from different tables binding it in one result, for you to understand it well, i posted my code & ERD below. thank you for your response. Please tell me where did I go wrong tnx :)
Here's the ERD of Mine:
and Here's the Code:
try
{
OleDbConnection Con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\MotoFix.mdb;");
OleDbCommand command = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
Con.Open();
command.CommandText = String.Format("SELECT fn.Customer + ' ' + mi.Customer + ' ' + ln.Customer as [CUSTOMER FULLNAME], fn.Cashier + ' ' + mi.Cashier + ' ' + ln.Cashier as [CASHIER FULLNAME], prodName.Product as [PRODUCT NAME], prodDescription.Product as [PRODUCT DESCRIPTION], prodBrand.Product as [PRODUCT BRAND], prodQuantity.Transaction as [PRODUCT QUANTITY], prodTotalPrice.Transaction as [SUBTOTAL], job.Personnel as [Referral] FROM Product , [Order] , [Transaction] , Customer , Cashier , Personnel WHERE prodCode.Product = ProdCode.Order AND orderNo.Order = orderNo.Transaction AND pID.Personnel = pID.Transaction AND custID.Customer = custID.Transaction AND userID.Cashier = userID.Transaction AND sDate.Transaction = '{0}'", strDate);
command.Connection = Con;
adapter.SelectCommand = command;
adapter.Fill(dt);
Con.Close();
Con.Dispose();
gridViewTransac_1.DataSource = dt;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message);
}
Additional information: No value given for one or more required parameters.
I'm not a good sql query builder but shouldn't it be TableName.FieldName not the other way around ?
Anyways try to put the TableName first:
SELECT Customer.fn + ' ' + customer.mi + ' ' + customer.ln as [CUSTOMER FULLNAME]
, Cashier.f. + ' ' + Cashier.mi + ' ' + customer.ln as [CASHIER FULLNAME]
, Product.prodName as [PRODUCT NAME]
, Product.prodDescription as [PRODUCT DESCRIPTION]
, Product.prodBrand as [PRODUCT BRAND]
, Transaction.prodQuantity as [PRODUCT QUANTITY]
, Transaction.prodTotalPrice as [SUBTOTAL]
, Personnel.job. as [Referral]
FROM Product
, [Order]
, [Transaction]
, Customer
, Cashier
, Personnel
WHERE Product.prodCode = Order.ProdCode
AND Order.orderNo = Transaction.orderNo
AND Personnel.pID= Transaction.pID
AND Customer.custID = Transaction.custID
AND Cashier.userID = Transaction.userID
AND Transaction.sDate = #transDate
For #transDate you should add:
command.Parameters.AddWithValue("#transDate", someDate);
or you could simply do as you did
I am new to C# and I have a C# program and an excel file exported from another program. How can I convert it to DateTime? Here is the code, where dtFrom and dtTo are DateTimePicker variables:
System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel.TableName = "MyExcelData";
string SourceConstr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + this.textBox1.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= " + this.dtFrom.Value.Month + ") GROUP BY Customer order by Customer";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);
I think it's probably not your only problem, but to answer your question, if you replace
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= " + this.dtFrom.Value.Month + ") GROUP BY Customer order by Customer";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
with
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= #Date) GROUP BY Customer order by Customer";
OleDbCommand command = new OleDbCommand(query);
command.Parameters.Add("#Date", OleDbType.DBTimeStamp).Value = this.dtFrom.Value;
OleDbDataAdapter data = new OleDbDataAdapter(command, con);
you'll probably be on the right track. Not 100% sure exactly which OleDbType you need, but you can see the available options easily enough.