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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a created a GridView in ASP.NET which is databound to a stored procedure.
In my stored procedure I have customerID which is from another table. How I can display name of the customer instead of the id number? Could anyone help me please?
This my code
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("usp_Counry"))
{
cmd.Parameters.AddWithValue("#Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
you may use join or subquery in your stored procedure to get customers' names if both tables have CustomerId column (column names may be different, values must match).
an example;
SELECT c.CustomerId, c.CustomerName, o.OrderId
FROM Orders AS o
INNER JOIN Customers AS c ON c.CustomerId = o.CustomerId
you can change table and column names as yours, also if you need to see all records included NULL values, may try OUTER JOIN
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 am trying to run a database query through c#. I am trying to pass a parameter into my sql statement but I am getting an exception saying invalid near #Agent_ID.
My code is like this
SqlCommand command = new SqlCommand("Select Csr_DISBURSEMENTDATE, Csr_AGENTNUMBER, Csr_TOTCURREARNINGS, Csr_MISCADJUSTMENTS, Csr_YTDTOTALCOMM, Csr_PAYMENTMETHOD From Cm_Opt_Csr_CommStatement_S " +
"inner join Cm_Opt_Con_Contract_S on Con_WritingCode = Csr_AgentNumber" +
"inner join Cm_Opt_Agt_Agent_S on agt_ID = Con_AgentID" +
"where Agt_ID = #AgentID");
command.Parameters.AddWithValue("#AgentID", Con_agentID);
command.Connection = conn;
SqlDataReader rdr = null;
rdr = command.ExecuteReader();
Con_agentID is a guid and in the database table the column which it maps to is a uniqueidentifer. I am stuck at this point. Could someone please point out the mistake in the syntax.
The exception thrown is
System.Data.SqlClient.SqlException: 'Incorrect syntax near 'Agt_ID'.'
You are missing spaces between words when you continue on to next line.
SqlCommand command = new SqlCommand("Select Csr_DISBURSEMENTDATE, Csr_AGENTNUMBER, Csr_TOTCURREARNINGS, Csr_MISCADJUSTMENTS, Csr_YTDTOTALCOMM, Csr_PAYMENTMETHOD From Cm_Opt_Csr_CommStatement_S " +
"inner join Cm_Opt_Con_Contract_S on Con_WritingCode = Csr_AgentNumber " +
"inner join Cm_Opt_Agt_Agent_S on agt_ID = Con_AgentID " +
"where Agt_ID = #AgentID");
command.Parameters.AddWithValue("#AgentID", Con_agentID);
command.Connection = conn;
SqlDataReader rdr = null;
rdr = command.ExecuteReader();
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
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 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 have the following problem performin a query in C#
I have the following method performing a simple query:
public List<DataModel.Vulnerability.VulnerabilitySolution> getVulnerabilitySolutionsList(int vulnId)
{
List<DataModel.Vulnerability.VulnerabilitySolution> result = new List<Vulnerability.VulnerabilitySolution>();
System.Data.Common.DbCommand command;
command = _connection.CreateCommand();
command.Connection = this._connection;
_strSQL = "VS.* FROM VulnerabilityAlertDocument_VulnerabilitySolution VAD_VS"
+ " INNER JOIN VulnerabilitySolution VS ON VAD_VS.VulnerabilitySolutionId = VS.Id"
+ " WHERE VAD_VS.VulnerabilityAlertDocumentId = #VULNID ";
addParameter(command, "#VULNID", vulnId);
command.CommandText = _strSQL;
_dt = fillDataTable(command);
DataModel.Vulnerability.VulnerabilitySolution vulnSolution;
foreach (DataRow row in _dt.Rows)
{
vulnSolution = new DataModel.Vulnerability.VulnerabilitySolution(row);
result.Add(vulnSolution);
}
return result;
}
The problem is that when try to execute this line: _dt = fillDataTable(command); it throw the following exception: Incorrect syntax near '*'
What could be the problem? If I try to execute the same query in SQL Server (replacing the value of the parameter) it work fine
How can I solve this issue?
You need to add the word select before the VS.*
_strSQL = "SELECT VS.* FROM VulnerabilityAlertDocument_VulnerabilitySolution VAD_VS"
+ " INNER JOIN VulnerabilitySolution VS ON VAD_VS.VulnerabilitySolutionId = VS.Id"
+ " WHERE VAD_VS.VulnerabilityAlertDocumentId = #VULNID ";