At the very begining please allow me to convey my regards and respect to everyone from StackOverFlow and all the users. You guys are doing a great job helping out people like us. Thankyou.
Now to my question: I am building a winforms application, for learning purpose, that: 1) Connects to a database and retrives results based on a query. 2) Query is generated by either: * The user clicking on various controls of the application or * The user writing the query herself and then executing it. 3) The results are displayed on the resultbox.
I am getting a SQLException of "Incorrect syntax near',' " The syntax generated is correct because: 1) I copied the generated query and executed it in SQL Management Studio. It executes there flawlessly. 2) I tried writing the query manually and then executing it but it still throws the same execption.
Please note that at the first stage of the appliaction development it had three controls only, a DataGridView control, a TextBox control and a Button control. At that stage I sucessfully generated results and displayed them. After I added the the extra features the application stopped working. I cannot understand why. I am using the same code I used in first stage of development, infact it is the same appliaction. I have hence made another application with the three basic controls mentioned earlier and copied the query generated by the primary appliaction and then executed it in the second application. It works there.
At the current stage the application throws the same execption if I use the generated query or if I write the query myself and then execute it. Here is my code. When user clicks on "Execute" button the LoadData method is called.
try
{
string ConnectString = "Data Source=(local); Initial Catalog=AdventureWorks2008;User ID=; Password=;Integrated Security=SSPI";
ConnectionObj.ConnectionString = ConnectString;//ConnectionObj is SQLConnection object defined in the same class as Loaddata()
ConnectionObj.Open();
ColumnNames = string.Empty;//ColumnNames is a String type defined in the same class as Loaddata()
foreach (string Name in ColumnNamesCheckedListBox.CheckedItems)//ColumnNamesCheckedListBox is a CheckedListBox control which lets user select ColumnNames from the corresponding table
{
ColumnNames = ColumnNames + Name + ", ";
}
int Length = ColumnNames.Length;
Length = Length - 2;//To remove the extra "," and "<space>" at the end of ColumnNames
string TempQuery = ColumnNames.Remove(length);
//User may use the query formed by the application or she may write her own query, hence using "string PossibleQuery"
//SelectQueryDropDownList is a ComboBox control holding the items "Select" "Update" "Insert" and "Delete". Used to generate the DML clause in the query
//Database_TreeView is a TreeView control that holds all the table names in the database. This is used to generate the "FROM" clause in the query
//QueryBox is a TextBox control that displays the generated query and if required lets the user write her own query manually
string PossibleQuery = SelectQueryDropDownList.SelectedItem.ToString() + TempQuery + " From " + Database_TreeView.SelectedNode.Name.ToString();
QueryBox.TempQuery = PossibleQuery;
string FinalQuery = QueryBox.Text; //incase the user modified the query at QueryBox manually
SqlDataAdapter DA = new SqlDataAdapter(FinalQuery, ConnectString);
SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DA);
DataTable Table = new System.Data.DataTable();
//EXCEPTION OCCURS AT THIS STATEMENT
DA.Fill(Table);
BindingSource Source = new BindingSource();
Source.DataSource = Table;
ResultBox.DataSource = Source;//ResultBox is a DataGridView control to display the results of the the query after execution (if any)
}
catch (Exception e)
{
MessageBox.Show(e.Message+"\nPlease try again","Error",MessageBoxButtons.OK);
}
finally
{
ConnectionObj.Close();
}
Here are the Screenshots: This is the query generated my the application. http://i60.photobucket.com/albums/h31/spiderclaws/Stack%20Over%20Flow/1ScreenShot2013-04-23at54453PM.png
This shows the exception generated. http://i60.photobucket.com/albums/h31/spiderclaws/Stack%20Over%20Flow/2ScreenShot2013-04-23at54550PM.png
Please help me out. Thanx. Regards, Gogol Prasad ([email protected]) P.S.- please excuse me for writing such a long post. I wanted it to be clear for everyone.
[Edit] Here is the info as requested: Please note that I am querying Person.BusinessEntityContact as per the screenshot...
FinalQuery=Select BusinessEntityID, PersonID, ContactTypeID From Person.BusinessEntityContact
StackTrace info:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at ADOBasicsWinFormsApp.Form1.loaddata() in C:\Users\Gogol\Documents\Visual Studio 2010\Projects\ADOBasicsConsoleApp\ADOBasicsWinFormsApp\Form1.cs:line 684