Row written to grid, database, but cannot delete row

I have a grid with a dataprocessor. I add a row to the grid, and verified the row gets written to the database. With focus remaining on the newly-added row, I then attempt to delete the row. It disappears from the grid, but not from the database. Refreshing the page causes the grid row I’d just deleted to reappear. It can then be successfully deleted from the grid and database after the refresh.

Here is the client code:

	<link rel='stylesheet' type='text/css' href='./dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.css'>
	<link rel='stylesheet' type='text/css' href='./dhtmlx/dhtmlxGrid/codebase/skins/dhtmlxgrid_dhx_skyblue.css'>
	<link rel='stylesheet' type="text/css" href='./dhtmlx/dhtmlxGrid/samples/common/css/style.css' media="screen" />	
	<script src='./dhtmlx/dhtmlxGrid/codebase/dhtmlxcommon.js'></script>
	<script src='./dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.js'></script> 
	<script src='./dhtmlx/dhtmlxGrid/codebase/dhtmlxgridcell.js'></script>		
	<script src='./dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_validation.js' type='text/javascript' charset='utf-8'></script>
	<script src='./dhtmlx/dhtmlxGrid/samples/common/data.js'></script>
	<script src='./dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_srnd.js'></script>
	<script src='./dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_filter.js'></script>
	<script src='./dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_form.js'></script>

<!-- Dataprocessor -->
	<script src='./dhtmlx/dhtmlxDataProcessor/codebase/dhtmlxdataprocessor.js'></script>
	<script src='./dhtmlx/dhtmlxDataProcessor/codebase/dhtmlxdataprocessor_debug.js'></script>

...

	myGrid2.setImagePath("./dhtmlx/dhtmlxGrid/codebase/imgs/");
	myGrid2.setHeader("Source,Type,Referrals,Contact Date,Follow Up,Cost,ContactID, ReferralID");
	myGrid2.attachHeader("#select_filter,#select_filter,#text_filter,#text_filter,#text_filter,#text_filter,,");
	myGrid2.setInitWidths("100,100,150,85,85,85,0,0");	
	myGrid2.enableAutoWidth(true);
	myGrid2.setColAlign("left,left,left,right,right,right,left,left");
//	See http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:initialization_from_xml 
//	http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:cells_manipulation#getting_select_box_collection
//	http://docs.dhtmlx.com/doku.php?id=dhtmlxconnector:filtration
//	for more info on populating select box collection.	
	myGrid2.setColTypes("co,co,ed,ed,ed,price,ed,ed");
	myGrid2.setColumnIds("0,1,2,3,4,5,6,7");
	myGrid2.enableValidation(true, true);		
	myGrid2.setColValidators(["NotEmpty","NotEmpty","NotEmpty",,,,"NotEmpty","NotEmpty"]);	
	myGrid2.setColSorting("str,str,str,date,date,int,str,str");
	myGrid2.enableMultiselect(true);	
	myGrid2.setSkin("dhx_skyblue");	
	myGrid2.init();
	myGrid2.enableSmartRendering(true);

//	Initiate referrals data processor. For more on dataprocessor, see http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:dataprocessor.
	myDataProcessor = new dataProcessor("FrstConnectorReferrals.php"); // use the connector file as a constructor parameter.		
	myDataProcessor.init(myGrid2);
	myDataProcessor.setUpdateMode("cell",true);
	myDataProcessor.setDataColumns("true,true,true,true,true,true,true,true");

Here is the dp code:

      $pwd=file_get_contents("C:\inetpub\wwwroot_DEV\pwd.txt");

	$connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>"COF_Objects_Dev", "LoginTimeout"=>15); // <- SS authentication
	$res = sqlsrv_connect( $serverName, $connectionInfo); // <- SS authentication

	if($res)
	{
		//echo "Connection established.\n";
		$qs = $_GET['clientid'];
		$grid = new GridConnector($res,"sqlsrv");
		$grid->dynamic_loading(100);
		// See http://docs.dhtmlx.com/doku.php?id=dhtmlxconnector:loading_editing_data for allowable SQL statement syntax.
		// and http://docs.dhtmlx.com/doku.php?id=dhtmlxconnector:jsondataconnector.

		if($grid->is_select_mode())
		{
			$thesql = "SELECT REPLACE(Source, CHAR(39), CHAR(96)) AS Source, REPLACE(Type, CHAR(39), CHAR(96)) AS Type, Referrals, (CASE WHEN CONVERT(varchar, ContactDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, ContactDate, 101) END) AS ContactDate, (CASE WHEN CONVERT(varchar, FollowupDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, FollowupDate, 101) END) AS FollowupDate, CONVERT(varchar,Cost,1) AS Cost, ContactID, ReferralID FROM COF_FrstReferrals_tbl WHERE ContactID='".$qs."'";
			$grid->render_sql($thesql, "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");
		}
		else
		{
			$grid->render_table("COF_FrstReferrals_tbl", "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");
		}		
		if($grid)
		{	
			//echo "rendering table ...\n";
		}
		else
		{
			//echo "grid not loaded!\n";
		}	
	}
	else
	{
		//echo "Connection could not be established.\n";
		die( print_r( sqlsrv_errors(), true));
	}	

Here is the dataprocessor log:

Current mode: cell
 Log:
 row 1360353874706 marked [inserted,valid]
 Initiating data sending for 1360353874706
 Initiating data sending for all rows
 Sending all data at once
 Server url: FrstConnectorReferrals.php?editing=true parameters
 Server response received details
<?xml version='1.0' ?><data><action type='inserted' sid='1360353874706' tid='' ></action></data>
 Action: inserted SID:1360353874706 TID:
 row 1360353874706 unmarked [updated,valid]
 row marked [deleted,valid]
 Initiating data sending for all rows
 Sending all data at once
 Server url: FrstConnectorReferrals.php?editing=true parameters
 Server response received details
<?xml version='1.0' ?><data><action type='deleted' sid='' tid='' ></action></data>
 Action: deleted SID: TID:
 row unmarked [updated,valid]
 row unmarked [updated,valid]

Hope you can help me. Thanks!

PS - I forget to include the add/delete code:

function addReferralRow() {
	var ind1 = window.prompt('enter Source', '');
	if (ind1 === null || typeof ind1 == "undefined")
		return;	
	var ind2 = window.prompt('enter Type', '');
	if (ind2 === null || typeof ind2 == "undefined")
		return;
	var ind3 = window.prompt('enter Referrals', '');
	if (ind3 === null || typeof ind3 == "undefined")
		return;
	var ind4 = window.prompt('enter Contact Date', '');
	if (ind4 === null || typeof ind4 == "undefined")
		return;
	var ind5 = window.prompt('enter Followup Date', '');
	if (ind5 === null || typeof ind5 == "undefined")
		return;
	var ind6 = window.prompt('enter Cost', '');
	if(ind6.length == 0) {
	ind6 = null; }
	//if (ind6 === null || typeof ind6 == "undefined")
	//	return;
	var ind7	= myGrid.getSelectedRowId();
	var myrefuid 	= myGrid2.uid();
	var ind8 	= myrefuid;
	
	myGrid2.addRow(myrefuid, [ind1,ind2,ind3,ind4,ind5,ind6,ind7,ind8], myGrid2.getRowsNum());
	myGrid2.selectRow(myGrid2.getRowIndex(myrefuid),false,false,true);

	if (confirm("Add another referral?")) 
		{			
	        addReferralRow();		        
		} 
	else    {
	        return false;
		} 	
}				

function deleteReferralRow() {
	var selId = myGrid2.getSelectedId();
	if (selId !== null)
		{	
		retType = confirm("Are you sure you want to delete the selected row(s)?"); 
	
		if (retType == true) 
			{
			myGrid2.deleteSelectedRows();		
			}
		}	
	else
		{
		alert("Please first select a row to delete.");
		}
	}		

Be problem description - it seems that new id was not correctly updated on client side, because of that delete operation failed.

And that is it

<?xml version='1.0' ?><data><action type='inserted' sid='1360353874706' tid='' ></action></data>

As you can see tid is not set.
It can be caused by using ReferralID as both ID and editable column.
if (ReferralID) is autoincrement field - you must not use it as editable column in the grid. To have default id updating functionality you need to have autoincrement field for id in the database, and do not show it as editable column in the grid

Thank you, Stanislav, I’ll see if I can fix it per your instructions. Your help is greatly appreciated!

Hi, Stanislav,

I haven’t been able to correct the problem. I changed the code from

	myGrid2.setColTypes("co,co,ed,ed,ed,price,ro,ed");

to

	myGrid2.setColTypes("co,co,ed,ed,ed,price,ro,ro");

but it made no difference.

I also changed the DP code from:

myDataProcessor.setDataColumns("true,true,true,true,true,true,true,true");

to

myDataProcessor.setDataColumns("true,true,true,true,true,true,true,false");

but that didn’t help either.

I think you may have misunderstood what I’m trying to do. I’m not trying to change the ReferralId through the DHTMLX interface. I’m also not using the DBMS to create an autoincrement ReferralID. I’m just trying to get a vanilla DHTMLX grid to work correctly with a back end Microsoft SQL Server database table. Everything works, except for the delete.

The ReferralID is set within the DHTMLX application:

	var myrefuid 	= myGrid2.uid();
	var ind8 	= myrefuid;
	myGrid2.addRow(myrefuid, [ind1,ind2,ind3,ind4,ind5,ind6,ind7,ind8], myGrid2.getRowsNum());

I’ve followed the DHTMLX code examples exactly, but still have this problem. The examples never seem to be complete, and they sometimes have typographical errors. Would you please, please, please give me a complete, functioning example of a grid successfully updating a backend SQL Server database (including both the client AND the PHP connector code)???

I’ve had this problem for a month now, and I would really like to get it solved. Thanks.

Changing to “ro” doesn’t help. Grid will try to save all columns in row. In your case it means it will save the id column with empty value ( as it is in grid ) instead of auto id generation. Probably my previous reply was confusing, I have mean that you can’t have ID as column in grid and expect that it will be autogenerated in the same time.

You can

a) fully remove that column from grid and use id autogeneration
or
b) use KeyGridConnector - which is designed for grid which can show|edit id column as part of data. But in such case id will not be autogenerated, you will need to provide it manuall. docs.dhtmlx.com/doku.php?id=dhtm … dconnector

If you need autogenerated id - use plan (a), if you have fixed id and just need to save it in DB - use plan (b)

Thank you once more, Stanislav. I think that clears up my confusion! I’ll try using an auto ID generated by the DBMS and hopefully that will correct the problem.

Hi, Stanislav,

I’m afraid the suggestions you made did not work. The grid row remains after deleting it.

I removed the ReferralID column from the grid. There are now only seven columns:

	myGrid2 = dhxTabbar.cells("a2").attachGrid();
	myGrid2.setImagePath("./dhtmlx/dhtmlxGrid/codebase/imgs/");
	myGrid2.setHeader("Source,Type,Referrals,Contact Date,Follow Up,Cost,");	
	myGrid2.attachHeader("#select_filter,#select_filter,#text_filter,#text_filter,#text_filter,#text_filter,");
	myGrid2.setInitWidths("100,100,150,85,85,85,0");	
	myGrid2.enableAutoWidth(true);
	myGrid2.setColAlign("left,left,left,right,right,right,");	
	myGrid2.setColTypes("co,co,ed,ed,ed,price,ro");	
	myGrid2.setColumnIds("0,1,2,3,4,5,6");
	myGrid2.enableValidation(true, true);		
	myGrid2.setColValidators(["NotEmpty","NotEmpty","NotEmpty",null,null,null,null]);		
	myGrid2.setColSorting("str,str,str,date,date,int");
	myGrid2.enableMultiselect(true);	
	myGrid2.setSkin("dhx_skyblue");	
	myGrid2.enableSmartRendering(true);
//	Re-sort grid if date field has been changed and cell editor closed. See http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:event_oneditcell 
	myGrid2.attachEvent("onEditCell", function(stage,rId,cInd,nValue,oValue){
	if (stage==2){
		myGrid2.sortRows(3, "date", "des");
		return true;
	}
	return true;
	});
	
	myGrid2.init();
	
//	Initiate referrals data processor. For more on dataprocessor, see http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:dataprocessor.
	myDataProcessor = new dataProcessor("FrstConnectorReferrals.php"); // use the connector file as a constructor parameter.	
	myDataProcessor.setTransactionMode("POST", true);
	myDataProcessor.setUpdateMode("cell",true);
	myDataProcessor.setDataColumns("true,true,true,true,true,true,false,false");	
	myDataProcessor.init(myGrid2);

Here is the dataprocessor code. I assume the ReferralID field is still needed in the SQL:


	if($res)
	{
		//echo "Connection established.\n";
		$qs = $_GET['clientid'];
		$grid = new GridConnector($res,"sqlsrv");
		$grid->dynamic_loading(100);

		if($grid->is_select_mode())	// loading data.
		{
			$thesql = "SELECT REPLACE(Source, CHAR(39), CHAR(96)) AS Source, REPLACE(Type, CHAR(39), CHAR(96)) AS Type, Referrals, (CASE WHEN CONVERT(varchar, ContactDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, ContactDate, 101) END) AS ContactDate, (CASE WHEN CONVERT(varchar, FollowupDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, FollowupDate, 101) END) AS FollowupDate, CONVERT(varchar,Cost,1) AS Cost, ContactID, ReferralID FROM COF_FrstReferrals_tbl WHERE ContactID='".$qs."'";
			$grid->render_sql($thesql, "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");
		}
		else	// update/insert/delete operations.
		{
			$grid->render_table("COF_FrstReferrals_tbl", "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");			
		}		
		if($grid)
		{	
			//echo "rendering table ...\n";
		}
		else
		{
			//echo "grid not loaded!\n";
		}	
	}
	else
	{
		//echo "Connection could not be established.\n";
		die( print_r( sqlsrv_errors(), true));
	}

As I have requested on severeal previous occasions, I would hugely appreciate it if you would send me client and data connector sample code which actually does grid row deletes correctly, using a back-end SQL Server database table. Everything else we’ve tried doesn’t appear to work. Thanks!

I assume the ReferralID field is still needed in the SQL:

Nope, you have it as ID , so you need not to duplicate it in fields list, the next will be enough

$grid->render_table("COF_FrstReferrals_tbl", "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID");         

Greetings again, Stanislav,

I don’t think that is working. By changing this:

		if($grid->is_select_mode())	// loading data.
		{
			$thesql = "SELECT REPLACE(Source, CHAR(39), CHAR(96)) AS Source, REPLACE(Type, CHAR(39), CHAR(96)) AS Type, Referrals, (CASE WHEN CONVERT(varchar, ContactDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, ContactDate, 101) END) AS ContactDate, (CASE WHEN CONVERT(varchar, FollowupDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, FollowupDate, 101) END) AS FollowupDate, CONVERT(varchar,Cost,1) AS Cost, ContactID, ReferralID FROM COF_FrstReferrals_tbl WHERE ContactID='".$qs."'";
			$grid->render_sql($thesql, "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");		
		}
		else	// update/insert/delete operations.
		{
			$grid->render_table("COF_FrstReferrals_tbl", "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");			
		}

to this:

		if($grid->is_select_mode())	// loading data.
		{
			$thesql = "SELECT REPLACE(Source, CHAR(39), CHAR(96)) AS Source, REPLACE(Type, CHAR(39), CHAR(96)) AS Type, Referrals, (CASE WHEN CONVERT(varchar, ContactDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, ContactDate, 101) END) AS ContactDate, (CASE WHEN CONVERT(varchar, FollowupDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar, FollowupDate, 101) END) AS FollowupDate, CONVERT(varchar,Cost,1) AS Cost, ContactID, ReferralID FROM COF_FrstReferrals_tbl WHERE ContactID='".$qs."'";
			$grid->render_sql($thesql, "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");		
		}
		else	// update/insert/delete operations.
		{
			//$grid->render_table("COF_FrstReferrals_tbl", "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID, ReferralID");
			$grid->render_table("COF_FrstReferrals_tbl", "ReferralID", "Source, Type, Referrals, ContactDate, FollowupDate, Cost, ContactID");				
		}

… the ReferralID is no longer written to the database when the record is added through the grid; it’s null.

I’m nearing the end of my DHTMLX evaluation period. If I can’t get something this simple working correctly and can’t get adequate help from DHTMLX, my company won’t buy the product, and it won’t advise other companies to do so, either.

I appreciate all your help, Stanislav, but PLEASE - I really need to see the code to a working example of a grid control performing add, change, delete, and read functions, as well as the sample PHP code (including the TSQL statements) using a back-end SQL Server database. There are too many variables for me to guess what the correct code should look like.

Thanks in advance.

Stanislav, also, I tried using the KeyGridConnector you suggested:

	require_once('./dhtmlxConnector_php_v15_120612/php/codebase/db_sqlsrv.php');
	require_once('./dhtmlxConnector_php_v15_120612/php/codebase/grid_connector.php');
...
		//$grid = new GridConnector($res,"sqlsrv");
		$grid = new KeyGridConnector($res,"sqlsrv");

but no data at all is retrieved as a result. Am I using the correct PHP dhtmlxConnector libraries? If not, where may I download them from, please? Thanks.

I tried using the keygrid_connector.php library, which also did not work:

	require_once('./dhtmlxConnector_php_v15_120612/php/codebase/db_sqlsrv.php');
	//require_once('./dhtmlxConnector_php_v15_120612/php/codebase/grid_connector.php');
	require_once('./dhtmlxConnector_php_v15_120612/php/codebase/keygrid_connector.php');

Thanks.

… the ReferralID is no longer written to the database when the record is added through the grid; it’s null.

Normal use-case expects that id of the record is set on server side as auto-generated field.

Stanislav, also, I tried using the KeyGridConnector you suggested:
Must be keygrid_connector.php

I tried using the keygrid_connector.php library, which also did not work:
with keygrid connector use-case grid must correctly user referalID as new id, save it in DB and update on client side accordingly. Please beware that you must use unique id values ( refferalId ) or grid will not be able to distinguish rows.

I really need to see the code to a working example of a grid control performing add, change, delete, and read functions

dhtmlx.com/x/download/regula … or_php.zip

Contains full set of samples, including full CRUD for grid.
The problem is - you are using quite rare case - with editable ID on client side. In 99% of use case you do not want to have editable id, as it easiest way to corrupt data relations.

Stan, thanks for all your help. I think I’ve figured out the problem. It works beautifully now.

It’s a pretty simple fix, once you know what’s the matter!

Thanks again.