Resolved Error even after changing datatype in both dataset and SQL Server Management Studio

Govind Sankar

Active member
Joined
May 15, 2020
Messages
42
Programming Experience
Beginner
I have a database and then a dataset in C# Visual Studio. In the database there is a table with a column called CardType. This was created by someone before me. And the value in this column is always 40. So he created it of type double.

I was told now that now the value has to be changed to SAR40 and not just 40. So now it is a string. So when I changed the value to SAR40 obviously there will be error when trying to add SAR40 to a column of type double. So first I went to SQL Server Management Studio and changed the type to nvarchar there. Then I come back to Visual Studio and changed the type of datatype of CardType to System.String.

Then when I run the program I still get an error

Parameter value could not be converted from string to double.
FormatException: The input string is in the wrong format.
How the program works is there are a bunch of small sections to fill data and each of the data goes into the database. Data like cardtype, hardwarerevision etc. So we create a datarow called workrow and then add this into the workrow.

workRow["CardType"] = type.Text; // CardType is string, I double checked by hovering the mouse over it and it shows as string.
workRow["Hardwarerevision"] = hw.Text;

Then we add the datarow into the dataset.
//add new data row
ds.UNIO_CPU.Rows.Add(workRow);

Finally we update the database using the dataset using a table adapter called DataGridAdapter_CPU
//write data to database
DataGridAdapter_CPU.Update(workRow);

So why do I still get an error?
I try to run the program with just 40 and the program is showing no error. It works fine. How is that possible. Because I changed both in the SQL Server Management Studio and dataset in Visual Studio to string type. So here it should show error. But it is not showing error. Whereas I try to enter SAR40 then it is showing error that you cant convert string to double.

The error is happening in the line DataGridAdapter_CPU.Update(workRow). Because i commented out this line and ran the rest. There was no error and the program worked but since this line was commented out, the database didnt recieve the new row. So the error happens when the tableadapter tries to add the datarow into the sql server database. But as I said I have changed CardType in the dataset to System.string and then also the colum CardType in SSMS to string. So is there something in the tableadapter I have to change. I am not an expert in this, I only know basic c# programming. Kindly do help me. Thank You.
 
Solution
Presumably you changed something in one place that didn't affect a change somewhere else. What you should done was made the change in the database and then regenerated the DataSet in the Data Sources window to make the change in all required places. I think the data type should change for the DataColumn but, if not, You can just delete the DataTable and have the whole thing regenerated from the database.
Presumably you changed something in one place that didn't affect a change somewhere else. What you should done was made the change in the database and then regenerated the DataSet in the Data Sources window to make the change in all required places. I think the data type should change for the DataColumn but, if not, You can just delete the DataTable and have the whole thing regenerated from the database.
 
Solution
Presumably you changed something in one place that didn't affect a change somewhere else. What you should done was made the change in the database and then regenerated the DataSet in the Data Sources window to make the change in all required places. I think the data type should change for the DataColumn but, if not, You can just delete the DataTable and have the whole thing regenerated from the database.
Thank you I did exactly that. I didnt delete the whole datatable but just the column CardType and I created a new CardType which is of string type and then configured the Dataset to include the new CardType column and it worked. So creating a new column worked but changing the old column didnt work. But earlier when I changed the type from double to string I didnt do anything with configuring the tableadapter, I just changed it and I thought table adapter works automatically. So i think i will do the old thing again that is change from double to string and then configure the tableadapter as well and see how it works to understand the problem. Anyway it worked for now which is all I need. Thank You.
 
Presumably you changed something in one place that didn't affect a change somewhere else. What you should done was made the change in the database and then regenerated the DataSet in the Data Sources window to make the change in all required places. I think the data type should change for the DataColumn but, if not, You can just delete the DataTable and have the whole thing regenerated from the database.
Yap I went to the old copy of the program which had the old CardType which was changed from double to string and there I reconfigured the table adapter again, and it worked. So changing in the table in VB and SSMS is not just enough. I need to configure the change in table adapter as well. That was my mistake.
 
Back
Top Bottom