Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am using entity framework database first concept.

when i insert values into database through my web page then this exception occurred.-

{"String or binary data would be truncated.\r\nThe statement has been terminated."}

Please help to solve this.

My database table-

CREATE TABLE [dbo].[Office](
[OfficeId] [uniqueidentifier] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Street] [varchar](100) NOT NULL,
[City] [varchar](75) NOT NULL,
[State] [varchar](2) NOT NULL,
[Zip] [varchar](5) NOT NULL,
[Phone] [varchar](9) NOT NULL,
[Fax] [varchar](9) NULL,
[CreatedDate] [datetime] NOT NULL,
[IsActive] [bit] NOT NULL,
CONSTRAINT [PK_Office_1] PRIMARY KEY CLUSTERED
(
[OfficeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
my class file method-
C#
public void AddOffice(string OfficeName, string StreetName, string CityName, String StateName, String ZipNumber, String PhoneNumber, string FaxNumber,DateTime CreatedOfficeDate,string  IsActiveOffice)
    {


            try
            {
                Office ObjOffice = new Office
                {
                    Name = OfficeName,
                    Street = StreetName,
                    City = CityName,
                    State = StreetName,
                    Zip = ZipNumber,
                    Phone = PhoneNumber,
                    Fax = FaxNumber,
                    CreatedDate=CreatedOfficeDate,
                    IsActive=bool.Parse(IsActiveOffice.ToString().Trim())

                };

                tde.AddToOffices(ObjOffice);
                tde.SaveChanges();
                //tde.AcceptAllChanges();


            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }


        }

Code behind code-
method-
C#
private void InsertOffice (string OfficeName, string StreetName, string CityName, String StateName, String ZipNumber, String PhoneNumber, string FaxNumber,DateTime CreatedOfficeDate,string   OfficeIsActive)
   {
       UserManager userMgr = new UserManager();
       oUserManager.AddOffice( OfficeName,  StreetName,  CityName,  StateName,  ZipNumber,  PhoneNumber,  FaxNumber, CreatedOfficeDate,OfficeIsActive);

   }



and call this method from following method-
C#
protected void btnSave_Click(object sender, EventArgs e)
   {
       try
       {
           string OfficeCreatedDate=System.DateTime.Now.ToString();
           string Active = "true";
           //Boolean IsActive = Convert.ToBoolean(Active.ToString().Trim());
          // Int32 IsActive = 1;
           InsertOffice(tbOfficeName.Text.ToString().Trim(), tbStreet.Text.ToString().Trim(), tbCity.Text.ToString().Trim(), tbState.Text.ToString().Trim(), tbZip.Text.ToString().Trim(), tbPhone.Text.ToString().Trim(), tbFax.Text.ToString().Trim(), Convert.ToDateTime(OfficeCreatedDate), Active);
           lblMsg.Visible = true;
           lblMsg.Text = "Office Created Sucessfully";
       }
       catch (Exception ex)
       {
           lblMsg.Visible = true;
           lblMsg.Text = ex.Message;

       }
   }



Thanks
Mukesh Bhagat.
Posted

1 solution

In the above problem there is little mistake-
change this into class method-
IsActive=Convert.ToBoolean(IsActiveOffice)

change into code behind-
Int32 IsActive = 1;


after that i solved my problem.

Regards
Mukesh Bhagat
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900