1) Orders
2) Product
Code
---------------------------------------------------------------------------------Code
--Code
-- Table structure for table `orders`Code
--Code
DROP TABLE IF EXISTS `orders`;Code
CREATE TABLE `orders` (Code
`order_id` int(10) unsigned NOT NULL auto_increment,Code
`prd_id` int(10) unsigned NOT NULL default '0',Code
`qty` int(10) unsigned NOT NULL default '0',Code
`cust_name` varchar(25) NOT NULL default '',Code
PRIMARY KEY (`order_id`)Code
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;Code
INSERT INTO `orders` VALUES (1,1,10,'Cust No.1');Code
INSERT INTO `orders` VALUES (2,1,14,'Cust No.2');Code
INSERT INTO `orders` VALUES (3,3,33,'Cust No.1');Code
INSERT INTO `orders` VALUES (4,2,50,'Cust No.2');Code
DROP TABLE IF EXISTS `product`;Code
CREATE TABLE `product` (Code
`id` int(10) unsigned NOT NULL auto_increment,Code
`code` varchar(5) NOT NULL default '',Code
`name` varchar(20) NOT NULL default '',Code
PRIMARY KEY (`id`),Code
UNIQUE KEY `UNIQUE_KEY` (`code`)Code
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;Code
INSERT INTO `product` VALUES (1,'IT101','Item 101');Code
INSERT INTO `product` VALUES (2,'IT102','Item 102');Code
INSERT INTO `product` VALUES (3,'IT103','Item 103');Code
INSERT INTO `product` VALUES (4,'IT104','Item 104');Code
---------------------------------------------------------------------------------my C# code is suppose to pull the info from these two table and show it in a datagridview.
I am using the Mysql .Net Connector (Latest version) to do the job.
if I run the query "select * from orders as o inner join product as p on o.prd_id = p.id" on a mysql console I get the following output.
C#
mysql> select * from orders as o inner join product as p on o.prd_id=p.id;Code
+----------+--------+-----+-----------+----+-------+----------+Code
| order_id | prd_id | qty | cust_name | id | code | name |Code
+----------+--------+-----+-----------+----+-------+----------+Code
| 1 | 1 | 10 | Cust No.1 | 1 | IT101 | Item 101 |Code
| 2 | 1 | 14 | Cust No.2 | 1 | IT101 | Item 101 |Code
| 4 | 2 | 50 | Cust No.2 | 2 | IT102 | Item 102 |Code
| 3 | 3 | 33 | Cust No.1 | 3 | IT103 | Item 103 |Code
+----------+--------+-----+-----------+----+-------+----------+BUT........ if I run the same query with C#, I get an exception
Code
---------------------------------------------------------------------------------SQL
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.Code
at System.Data.DataTable.EnableConstraints()Code
at System.Data.DataTable.set_EnforceConstraints(Boolean value)Code
at System.Data.DataTable.EndLoadData()Code
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)Code
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)Code
at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)Code
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)Code
at System.Data.DataTable.Load(IDataReader reader)Code
at MySQLQuery.DAL.returnDataInDataTable(String sqlQuery) in C:\Documents and Settings\User1\Desktop\CSharp\MySQLQuery\DAL.cs:line 42Code
---------------------------------------------------------------------------------but If I run just "select * from product" on my C# side this thing works!
C# Code snip:
Code
---------------------------------------------------------------------------------Code
private void btnRunQuery_Click(object sender, EventArgs e)Code
{Code
tryCode
{Code
string query = tbxQuery.Text.ToString();Code
BOL bol = new BOL();Code
DataTable dt = new DataTable();Code
//bol.GetDataFromMysqlTable runs the query and returns a datatableCode
dt = bol.GetDataFromMysqlTable(query);Code
dgvMysqlData.DataSource = dt;Code
}Code
catch (Exception ex)Code
{Code
tbxExceptions.Text = ex.Message.ToString();Code
}Code
}Code
---------------------------------------------------------------------------------So the million dollar question is... what is wrong with my query. As far as I am concerned the query works & what I am doing wrong here!
Please let me know as my brain is running out of brain cells..
Thanks all for keeping this forum so useful.
MT

Vijaya KadiyalaRank #1,085 · 1.8K reputationPosted Feb 6, 2009, 8:10 AM EST
I dont see any issue in the code, but check out the below link for a good suggestion to identify what's causing this issue.
http://scootville.blogspot.com/2006/07/failed-to-enable-constraints-one-or.html
Thanks
Vijaya Kadiyala
http
://dotnetvj.blogspot.comMirko Meier80 reputationPosted Feb 15, 2009, 10:46 AM EST
If this way doesn't work, have a look at the MSDN and the datatable with more than one primary key.