Converted bigint to varchar in MSSQL

I have this ContactNo column, and others are similar to this eg FaxNo , etc.

The problem is in MSSQL, these columns have been identified as bigint. and I changed them to varchar in MSSQL using the management studio.

enter image description here

The only reason I want to change it from int to varchar / string is so that I can start "0". As in the beginning, first enter "0".

I thought that I also needed to make changes to the dataset, so I also updated the dataset in WPF. In datasets, the same column for a single table has been changed from System.Int64 to System.String

enter image description here

But even I still get this error.

 Specified Cast is not valid 

enter image description here

It works fine if I change the inverse types to the types that were before .. but these large int types do not allow zeros ..

when i do the debugging. See this yellow highlight. after that, it goes directly to the exception error.

enter image description here

== - == - == - == - == - =

** Update 1: **

enter image description here

enter image description here

+7
sql-server dataset
source share
5 answers

Edit

After going through my code, which is not shown here, two areas emerged that were problematic.

One of them is data annotations, and the other was still running on longint in xaml.cs

When changing data types like this, it can be very difficult, and we need to go through each file in the code where this field is processed.

I'm glad that you solved your problem, we all understand the disappointment of a brick wall hit :)


Perhaps this is caused by the installation in MSSERVER:

Go to options and disable . Prevent saving changes that require saving the table. Since you can change the table in some parts of your project, but the changes are not saved in the database. Thus, there will be a cast exception, since it is looking for bigint, not varchar.

enter image description here

+1
source share

I suspect you have a field in AgentAccount that you have not updated to be a string, and it is still int. Therefore, casting does not work. Select AgentAccount in the code, press F12 to go to the definition, and then update the field as a string. Then everything should work.

+2
source share

Since you made changes to the base database, all you need to do in your program is to restore your typed dataset code. This should solve all such problems.

Try the following:

Close all windows and open Dataset1.xsd (designer). Do something on the surface of the designer that makes him recover the code. In most cases, just moving one of the tables should do the trick a bit; but if it's not just right-clicking and adding a dummy adapter, then save it, then delete and save again. (The goal is to make it again generated by automatically generated code).

Repeat this process for more typed data sets that you may have. (I see a single file in the screenshot named RMSDatabase2Dataset. Xsd).

Finally, save everything and complete a complete rebuild of the project.

+2
source share

If you want to display leading zeros only in the user interface, you can use the string format for bindings or you can also use converters. But if you need to store values ​​with leading zeros in the database, then it is better to change the type to varchar.

Second, do you use the Datasets or LINQ to SQL classes? The sample code has both, and it seems, I assume that you receive data using adapters in the DataClassesDataContext.AgentAccount property.

Make sure the query returns data, otherwise you will not be able to get an exception, as there will be no object to create.

Performing the same steps in a simple dataset, everything works fine, because when you modify Dataset.ContactNo and save the file, it reflects in all the appropriate places.

+2
source share

From the DataSet1.xsd image you uploaded, the MaxLength ContactNo property is -1 .

After changing ContactNo from System.Int64 to System.String you should also change ContactNo MaxLength from -1 to 30 .

+1
source share

All Articles