-1

I have a string variable which is receiving a Guid. I need to convert this string back to Guid as the database field is of type Guid.

I tried using:

Guid myGuid = Guid.Empty;;
myGuid = Guid.Parse(myId);

But keep getting Format Exception; Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Below is my code:

 public string addVehicleDetails(string vname, string vtype, string regNumber, string ftype, string fefficiency, string insby, DateTime datecreated, string myId)
        {

            string result = string.Empty;
            tblVehicleDetail myVehicleDetails = new tblVehicleDetail();
          //  Guid myGuid = Guid.Empty;
            System.Guid myGuid = new Guid(myId);
           // myGuid = Guid.Parse(myId);
            try {

               // Add details into vehicle details
                myVehicleDetails.VehicleName = vname;
                myVehicleDetails.VehicleType = vtype;
                myVehicleDetails.RegistrationNumber = regNumber;
                myVehicleDetails.FuelType = ftype;
                myVehicleDetails.FuelEfficiency = fefficiency;
                myVehicleDetails.InsuranceBy = insby;

                myVehicleDetails.DateCreated = datecreated;
                myVehicleDetails.Guid = myGuid;

                dc.tblVehicleDetails.InsertOnSubmit(myVehicleDetails);

                dc.SubmitChanges();

                return result = vname;

            }
            catch(Exception exp)
            {
                exp.ToString();
                return result = "Error in Adding Vehicle Details.";
            }
        }

Any solution for this?

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
Trupti
  • 597
  • 4
  • 8
  • 17

1 Answers1

0

This Format Exception tells you that your input(String myId) is not in a recognized format.

You should handle exceptions clearly your input string format is incorrect so use Guid.TryParse(This method returns false if input is null or not in a recognized format, and does not throw an exception.) . Here.

Also check for Guid.TryParseExact method .

  myGuid = Guid.Parse(myId); 
  if (Guid.TryParse(myId, out newGuid))
    {
     try 
      { 
       myVehicleDetails.DateCreated = datecreated; 
       myVehicleDetails.Guid = myGuid; 
       dc.tblVehicleDetails.InsertOnSubmit(myVehicleDetails); 
       dc.SubmitChanges(); 
       } 
     }
     else
      {
        //Input string is not in correct format
      }
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
  • after trying this i could execute else block i.e input string was not in correct format – Trupti Dec 16 '13 at 09:20
  • @Trupti You should check your input string format, Go through this you can specify format of input parameter in your case your `myID` http://msdn.microsoft.com/en-us/library/system.guid.tryparseexact.aspx – Suraj Singh Dec 16 '13 at 09:30
  • actually while getting the string it is coming with slashes like "\"********-****-****-****-************\"" may be because of that i am facing the problem – Trupti Dec 16 '13 at 10:43
  • @Trupti Please post your code where you are initializing your `Guid` and converting it to string . and please post your string value.Otherwise it's just a wild-goose chase. – Suraj Singh Dec 16 '13 at 10:54
  • okkk ... i could find what was going wrong ..it was because of those slashes ...once i remove those slashes i can get the output properly. this code is correct to convert string to Guid myGuid = Guid.Parse(myId); – Trupti Dec 17 '13 at 05:14