Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i'm a student in a vocational high school and i'm still new to programming, i have an assignment to create a application using c#, and i have a problem in joining 3 tables to display on datagridview..
i have tried the query on mySql it works just fine, but when i applied it in my c# line of code it didnt work it shows "no database selected", can somebody help me on this, here's my full code
string constring = "datasource=localhost;port=3306;username=root;password=root";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("select book_detail.id_bookdetail, location.location_id, location.location_name, book.book_id, book.title from location inner join book_detail on location.location_id = book_detail.location_id inner join book on book_detail.book_id = book.book_id; ", conDataBase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
transfer_view.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Your connection string should specify a database name:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
^^^^^^^^^^^^^^^^^^^
(Port 3306 is the default MySql port)
Ref. MySQL connection strings
Do a "use mydbname"
Perhaps it is the last optional parameter in prior string that u are not providing
Can do "select database()" to show current db in use
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have this comamand and that error, in data i have zip code 79000 and table name site
private void Crt_clck_Click(object sender, EventArgs e)
{
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT CMC, [Site Name], [Phone Number], Zip_Code FROM site Where Zip_Code'" + Zipcode.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
can you help me with this
Change your sql statement to
cmd.CommandText = "SELECT CMC, [Site Name], [Phone Number], Zip_Code FROM site Where Zip_Code = '" + Zipcode.Text + "'";
You are missing the = which is needed for the syntax to be correct.
But you should think about using parameter instead to avoid SQL Injection.
Why do we always prefer using parameters in SQL statements? could be interesting for this, too.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Hi i have problem with execute Query with Parameter in access database:
OleDbConnection cnn;
OleDbCommand cmdselect2;
string sqlselect2 = null;
string baza = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + #"L:\Windykacja\Sdro\Projekt\projekt.accdb";
connetionString = baza;
sqlselect2 = "SELECT count(POS_Pesel_regon) as Suma FROM POS WHERE POS_Pesel_regon = #PR";
cnn = new OleDbConnection(connetionString);
cnn.Open();
cmdselect2 = new OleDbCommand(sqlselect2, cnn);
Int32 PR1 = Convert.ToInt32(cmdselect2.ExecuteScalar());
cmdselect2.Parameters.AddWithValue("#PR", textBox6.Text);
cmdselect2.Dispose();
cnn.Close();
It's say that my paramter is missing
In insert it works perfectly :)
will be thankfull for any sugestions.
cheers
Because you try to execute your command before you add your parameter. Change those lines
Int32 PR1 = Convert.ToInt32(cmdselect2.ExecuteScalar());
cmdselect2.Parameters.AddWithValue("#PR", textBox6.Text);
to
cmdselect2.Parameters.AddWithValue("#PR", textBox6.Text);
Int32 PR1 = Convert.ToInt32(cmdselect2.ExecuteScalar());
A few things more;
Use using statement to dispose your connection and command automatically instead of calling Close or Dispose methods manually.
Don't use AddWithValue as much as you can. It may generate unexpected and surprising results sometimes. Use Add method overload to specify your parameter type and it's size.
using(var cnn = new OleDbConnection(connetionString))
using(var cmdselect2 = cnn.CreateCommand())
{
cmdselect2.CommandText = #"SELECT count(POS_Pesel_regon) as Suma FROM POS
WHERE POS_Pesel_regon = #PR";
cmdselect2.Parameters.Add("#PR", OleDbType.VarChar).Value = textBox6.Text;
// I assumed your column type as VarChar
cnn.Open();
int PR1 = (int)cmdselect2.ExecuteScalar();
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm getting this error when I'm trying to open a form in my C# App and I don't know what might be the problem. The error is in this line of code DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader());
Error:
{"Incorrect syntax near ','."}
public void popTraseu()
{
string cs = "Data Source=CODRINMA\\CODRINMA;Initial Catalog=TrafficManager;Integrated Security=True";
string select = "ats.NrOrdine, a.IDAutogara, a.Denumire as Autogara, o.Oras as Oras, o.Judet FROM Curse c INNER JOIN Trasee t on c.IDTraseu=t.IDTraseu INNER JOIN AutogariTrasee ats on t.IDTraseu=ats.IDTraseu INNER JOIN Autogari a on ats.IDAutogara=a.IDAutogara INNER JOIN Orase o on a.IDOras=o.IDOras where IDCursa=#IDCursa ORDER BY ats.NrOrdine";
try
{
using (SqlConnection con = new SqlConnection (cs))
{
con.Open();
SqlCommand cmd = new SqlCommand(select, con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("IDCursa", int.Parse(grdCurse.CurrentRow.Cells[0].FormattedValue.ToString()));
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
con.Close();
grdDetalii.DataSource = dt;
}
}
catch (Exception er) { MessageBox.Show(er.Message); }
It looks like you forgot to put select at the beginning of your query.
string select = "select ats.NrOrdine, a.IDAutogara, a.Denumire as Autogara, o.Oras as Oras, o.Judet FROM Curse c INNER JOIN Trasee t on c.IDTraseu=t.IDTraseu INNER JOIN AutogariTrasee ats on t.IDTraseu=ats.IDTraseu INNER JOIN Autogari a on ats.IDAutogara=a.IDAutogara INNER JOIN Orase o on a.IDOras=o.IDOras where IDCursa=#IDCursa ORDER BY ats.NrOrdine";
Your select does not start with a SELECT. Hence the syntax of your SQL statement is invalid.
Also note this line:
cmd.Parameters.Clear();
is completely superfluous. There are no parameters in a new instance.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to get a label to display how many entries exist in a database. And I'm using the most ridiculously simple FROM I can imagine. And I'm getting spammed with "Syntax error in FROM clause" rather than having my label update. Syntax is an error I get a lot when I use a system reserved name for a table or column. But the table name I'm using works in other statements, so I assume that's not the issue, and it's the ONLY variable. Unless it's something other than the FROM and it's lying to me, which is entirely possible...
if (DateTime.Now.Millisecond > 500)
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=access.mdb";
conn.Open();
OleDbCommand cmmd = new OleDbCommand("SELECT * FROM probe)", conn);
using (OleDbDataReader myReader = cmmd.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(myReader);
int count = dt.Rows.Count;
lblCount.Text = count.ToString();
conn.Close();
}
}
catch (OleDbException expe)
{
MessageBox.Show(expe.Message);
}
}
}
SELECT * FROM probe)
should be
SELECT * FROM probe
?
Change
OleDbCommand cmmd = new OleDbCommand("SELECT * FROM probe)", conn);
to
OleDbCommand cmmd = new OleDbCommand("SELECT * FROM probe", conn);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
con = new MySqlConnection(cs);
con.Open();
cmd = new MySqlCommand("SELECT (invoiceNo) as [Invoice No],(invDate) as [inv Date],
(sales.CustomerID) as [Customer ID],(CustomerName) as [Customer Name],
(GrandTotal) as [Grand Total],(TotalPayment) as [Total Payment],(PaymentChange) as
[Payment Change] from sales,customer where sales.CustomerID=customer.CustomerID
and invDate between #" + dtpInvoiceDateFrom.Text + "# And #" +
dtpInvoiceDateTo.Text + "# order by invDate desc", con);
MySqlDataAdapter mySDAp = new MySqlDataAdapter(cmd);
DataSet myDatSet = new DataSet();
mySDAp.Fill(myDatSet, "sales");
mySDAp.Fill(myDatSet, "customer");
dataGridView1.DataSource = myDatSet.Tables["customer"].DefaultView;
dataGridView1.DataSource = myDatSet.Tables["sales"].DefaultView;
The error statement is : You have an error in your SQL syntax check the manual that corresponds to your MySql server version for the right syntax to use near '[InvoiceNo],(invDate) as [inv Date],(sales.CustomerID) as [Customer ID],(Custom' at line 1
MySQL does not allow square brackets around table of column names.
Please refer to the link
http://www.convert-in.com/mssql-to-mysql-queries.htm