I am trying to load data from a database into a single DataTable and display it in a DataGridView. When running the code it gives me the exception Incorrect syntax near the keyword 'FROM'
Code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection CON = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\matricsneo\Desktop\New SAD\Villanueva\Villanueva\VillanuevaDB.mdf;Integrated Security=True;User Instance=True");
DataTable DT = new DataTable();
SqlDataAdapter SDA1 = new SqlDataAdapter("SELECT FROM * M101", CON);
SDA1.Fill(DT);
SqlDataAdapter SDA2 = new SqlDataAdapter("SELECT FROM * M102", CON);
SDA2.Fill(DT);
SqlDataAdapter SDA3 = new SqlDataAdapter("SELECT FROM * M103", CON);
SDA3.Fill(DT);
SqlDataAdapter SDA4 = new SqlDataAdapter("SELECT FROM * M104", CON);
SDA4.Fill(DT);
SqlDataAdapter SDA5 = new SqlDataAdapter("SELECT FROM * M105", CON);
SDA5.Fill(DT);
SqlDataAdapter SDA6 = new SqlDataAdapter("SELECT FROM * M106", CON);
SDA6.Fill(DT);
SqlDataAdapter SDA7 = new SqlDataAdapter("SELECT FROM * M107", CON);
SDA7.Fill(DT);
SqlDataAdapter SDA8 = new SqlDataAdapter("SELECT FROM * M108", CON);
SDA8.Fill(DT);
SqlDataAdapter SDA9 = new SqlDataAdapter("SELECT FROM * M109", CON);
SDA9.Fill(DT);
SqlDataAdapter SDA10 = new SqlDataAdapter("SELECT FROM * M110", CON);
SDA10.Fill(DT);
SqlDataAdapter SDA11 = new SqlDataAdapter("SELECT FROM * M111", CON);
SDA11.Fill(DT);
SqlDataAdapter SDA12 = new SqlDataAdapter("SELECT FROM * M112", CON);
SDA12.Fill(DT);
SqlDataAdapter SDA13 = new SqlDataAdapter("SELECT FROM * M113", CON);
SDA13.Fill(DT);
SqlDataAdapter SDA14 = new SqlDataAdapter("SELECT FROM * M114", CON);
SDA14.Fill(DT);
dataGridView1.DataSource = DT;
}
Can anyone please help me with this issue?
check your SELECT strings - those should be SELECT * FROM TABLE instead of SELECT FROM * TABLE
Related
Getting Error in Csharp :
string sq = "select EmpName,contactnumber,dob,Address from tbl_emp_details where empname = #empname";
SqlCommand cmd = new SqlCommand(sq, con);
cmd.Parameters.AddWithValue("#empname", comboBox1.SelectedText);
SqlDataAdapter adp = new SqlDataAdapter(sq, con);
// com.Parameters.AddWithValue("#empname",Name.SelectedText);
DataTable dt = new DataTable();
adp.Fill(dt);
Name.Text = dt.Rows[0]["Empname"].ToString();
You're creating the data adapter using the original SQL and not the command (which includes the parameter) that you create.
Change
SqlDataAdapter adp = new SqlDataAdapter(sq, con);
to
SqlDataAdapter adp = new SqlDataAdapter(cmd);
When I type in my search box my Data grid view doesn't show the data that i typed in even though its in the database. This is my codes for the search box
MySqlConnection con = new MySqlConnection(MyConnectionString);
con.Open();
MySqlDataAdapter adapt = new MySqlDataAdapter();
con = new MySqlConnection(MyConnectionString);
adapt = new MySqlDataAdapter("SELECT * from tblregister where FirstName like '" + textBox1.Text + "%'", con);
DataTable ds = new DataTable();
DataSet dt = new DataSet();
adapt.Fill(dt);
dataGridView1.DataSource =ds;
I have got this code below, which should connect 2 tables (ZAJSLUZ and KLISLUZ) but I need to add into it condition to select only those from ZAJSLUZ where column AKCE = zakce.Text
Would someone improve my code please ?
It gives me error that there is "bad syntax near ="
DataTable dt = new DataTable();
//SqlDataAdapter SDA = new SqlDataAdapter("select * from zajsluz",spojeni);
SqlDataAdapter SDA = new SqlDataAdapter("SELECT zajsluz.akce ,zajsluz.text,klisluz.pocet FROM zajsluz RIGHT JOIN klisluz ON zajsluz.ID=klisluz.id WHERE zajsluz.akce="+zakce.Text, spojeni);
SDA.Fill(dt);
dtg_ksluzby.DataSource = dt;
check if zakce.Text is a valid string before.
string sZakce = string.Empty;
if(zakce != null && zakce.Text != null)
{
sZakce = zakce.Text;
}
string sQuery = string.Format("SELECT zajsluz.akce ,zajsluz.text,klisluz.pocet FROM zajsluz RIGHT JOIN klisluz ON zajsluz.ID=klisluz.id WHERE zajsluz.akce= '{0}'", sZakce)
SqlDataAdapter SDA = new SqlDataAdapter(sQuery, spojeni);
i also suggest you to use the using block if you work with DataAdapters, so your adapter is disposed automatically.
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM table", con))
{
// use your adapter a
}
Change your line like this.
SqlDataAdapter SDA = new SqlDataAdapter("SELECT zajsluz.akce ,zajsluz.text,klisluz.pocet FROM zajsluz RIGHT JOIN klisluz ON zajsluz.ID=klisluz.id WHERE zajsluz.akce='"+zakce.Text+"'", spojeni);
...zajsluz.akce=+"zakce.Text,...
you might want to change it into
...zajsluz.akce='"+zakce.Text+"'",...
Change your line to
SqlDataAdapter SDA = new SqlDataAdapter("SELECT zajsluz.akce ,zajsluz.text,klisluz.pocet FROM zajsluz RIGHT JOIN klisluz ON zajsluz.ID=klisluz.id WHERE zajsluz.aakce='" + zakce.Text + "'", spojeni);
SqlDataAdapter SDA = new SqlDataAdapter("SELECT zajsluz.akce ,zajsluz.text,klisluz.pocet FROM zajsluz RIGHT JOIN klisluz ON zajsluz.ID=klisluz.id WHERE zajsluz.akce= '" +zakce.Text +"'", spojeni);
hi everyone i have this code here
SqlConnection con = new SqlConnection(Properties.Settings.Default.StoreDBConnection);
SqlDataAdapter da = new SqlDataAdapter();
private void Form1_Load(object sender, EventArgs e)
{
String test = DateTime.Now.ToString("yyyy-MM-dd");
var dept = Properties.Settings.Default.GiftDepartment;
MessageBox.Show(""+test+"");
SqlCommand cmd = new SqlCommand("SELECT DeptNo,DeptSales,DeptCustomers FROM StoreSalesDetail where DeptNo="+dept+" and ProcDate>="+test+"", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet1 ds = new DataSet1();
da.Fill(ds);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables[1]));
this.reportViewer1.RefreshReport();
}
Sample data in my table
2013-05-03 00:00:00.000,2,120.0000,1,1
2013-05-04 00:00:00.000,2,50.0000,1,1
test returns the date 2013-05-04 so why does my report return both date in the >= query?
try using datediff for more precision. Do wrap input value [test] in single quotes.
SqlCommand cmd = new SqlCommand("SELECT DeptNo,DeptSales,DeptCustomers FROM StoreSalesDetail where DeptNo="+dept+" and datediff(d, '" + test + "', ProcDate) >=0", con);
I have the following C# code:
public string TargetDate()
{
SqlConnection con =
new SqlConnection("Server=localhost;Database=Timer;Trusted_Connectopn=True");
SqlCommand cmd = new SqlCommand("select * from Timer");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();
}
but I get error at the: new DataSet(cmd,con); ...
the error: CS1502: The best overloaded
method match for
'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo,
System.Runtime.Serialization.StreamingContext)'
has some invalid arguments
What is could be the problem?
Try this:
SqlConnection con = new SqlConnection
("Server=localhost;Database=Timer;Trusted_Connection=True");
SqlCommand cmd = new SqlCommand("select * from Timer", con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
This is even better:
DataTable dataTable = new DataTable();
using(SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))
using(SqlCommand command = connection.CreateCommand())
{
command.CommandText = "select * from Timer";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
dataTable.Load(reader);
}
You've got the wrong constructor for the DataSet. Try this
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(con);
It seems you have mixed up the constructors:
Try the following:
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(con);
Hope that helps