CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2013
    Posts
    1

    DataType set to System.String but get error stating System.Int32

    The problem I'm having, as stated in the title, is that when my program reaches the point where it is trying to add rows to the table in my dataset I get this error:

    Code:
    System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Input string was not in a correct format.Couldn't store <> in colFName Column.  Expected type is Int32.
      Source=System.Data
      StackTrace:
           at System.Data.DataColumn.set_Item(Int32 record, Object value)
           at System.Data.DataRow.set_Item(DataColumn column, Object value)
           at System.Data.DataRow.set_Item(String columnName, Object value)
           at Webb_Week4.Manager.Save() in z:\Documents\POS2\Webb_Week4\Webb_Week4\Main.cs:line 257
           at Webb_Week4.Main.btnSave_Click(Object sender, EventArgs e) in z:\Documents\POS2\Webb_Week4\Webb_Week4\Main.cs:line 66
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at Webb_Week4.Program.Main() in z:\Documents\POS2\Webb_Week4\Webb_Week4\Program.cs:line 19
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.FormatException
           HResult=-2146233033
           Message=Input string was not in a correct format.
           Source=mscorlib
           StackTrace:
                at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
                at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
                at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
                at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
                at System.Data.DataColumn.set_Item(Int32 record, Object value)
           InnerException:
    Here is my source code. As you can see I never even use the Int32 type in my table. Any thoughts on what is wrong?

    Code:
        public partial class Main : Form
        {
    
            private DataSet dataSet;
            public string t1name = "Employee";
            public string t2name = "Manager";
            
    
            public Main()
            {
                
    
                InitializeComponent();
                dataSet = new DataSet();
                
            }
    
            private void cmbState_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            //occurs on clicking save button
            private void btnSave_Click(object sender, EventArgs e)
            {
                //grabs strings from the txt boxes
                string pos = cmbPosition.Text;
                string first = txtFName.Text;
                string last = txtLName.Text;
                string phone = txtPhone.Text;
                string email = txtEMail.Text;
                string addr = txtAddr.Text;
                string city = txtCity.Text;
                string state = cmbState.Text;
                string zip = txtZip.Text;
                string area = cmbArea.Text;
                DateTime date = dtpSDate.Value.Date;
    
              
    
                //saves information to dataset and opens second form
               
                    Manager Manage = new Manager(first, last, phone, email, addr, city, state, zip, pos, area, date);
                    //sets manager dataset to dataSet
                    Manage._ds = dataSet;
                    Manage.Save();
                    tableForm();
    
            }
    
            
           
    
            
    
            private void tableForm()
            {
                Point value = this.Location;
                Table table = new Table(dataSet);
    
                table.Show();
                table.Left = value.X + this.Width;
                table.Top = value.Y;
    
            }
    
            private void Main_Load(object sender, EventArgs e)
            {
                // The following code creates two tables "Employee" and "Manager"
                // It then creates the columns for each before linking them through
                // The Employee primary key of colPhone
    
                MakeTable(t1name);
    
                string colName = "colFName";
                string colCaption = "First";
                string colType = "System.String";
                bool colUnique = false;
                DataTable Table = dataSet.Tables[t1name];
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colLName";
                colCaption = "Last";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colPhone";
                colCaption = "Phone";
                colUnique = true;
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colEMail";
                colCaption = "E-Mail";
                colUnique = false;
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colAddr";
                colCaption = "Address";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colCity";
                colCaption = "City";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colState";
                colCaption = "State";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colZip";
                colCaption = "Zip";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
    
                MakeTable(t2name);
    
                colName = "colPosition";
                colCaption = "Position";
                colType = "System.String";
                colUnique = false;
                Table = dataSet.Tables[t2name];
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colArea";
                colCaption = "Area";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colName = "colSDate";
                colCaption = "Start Date";
                colType = "System.DateTime";
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                colType = "System.String";
                colName = "colPhone";
                colCaption = "Phone";
                colUnique = true;
                MakeCol(colName, colCaption, colType, colUnique, Table);
    
                //displays the second form of the program
                this.Location = new Point(100, 100);
    
    
            }
            public void MakeTable(String tableName)
            {
                DataTable dt = new DataTable(tableName);
                dataSet.Tables.Add(dt);
            }
    
            public void MakeCol(String colName, String colCaption, String colType, bool colUnique, DataTable Table)
            {
                //creates column
                DataColumn col = new DataColumn();
                col.DataType = System.Type.GetType(colType);
                col.ColumnName = colName;
                col.AutoIncrement = true;
                col.Caption = colCaption;
                col.ReadOnly = true;
                col.Unique = colUnique;
                Table.Columns.Add(col);
            }
        }
    
        public class Employee
        {
            protected string strFName;
            protected string strLName;
            protected string strPhone;
            protected string strEMail;
            protected string strAddr;
            protected string strCity;
            protected string strState;
            protected string strZip;
    
            public Employee()
            {
                strFName = "null";
                strLName = "null";
                strPhone = "null";
                strEMail = "null";
                strAddr = "null";
                strCity = "null";
                strState = "null";
                strZip = "null";
            }
    
            public Employee(string first, string last, string phone, string email, string addr, string city, string state, string zip)
            {
                strFName = first;
                strLName = last;
                strPhone = phone;
                strEMail = email;
                strAddr = addr;
                strCity = city;
                strState = state;
                strZip = zip;
            }
    
            
    
        }
    
        public class Manager : Employee
        {
            protected string strPosition;
            protected string strArea;
            protected DateTime varDate;
            private DataSet ds;
    
            public DataSet _ds
            {
                set { ds = value; }
    
            }
    
            public Manager() { }
    
            public Manager(string first, string last, string phone, string email, string addr, string city, string state, string zip, string position, string area, DateTime date)
            {
                strFName = first;
                strLName = last;
                strPhone = phone;
                strEMail = email;
                strAddr = addr;
                strCity = city;
                strState = state;
                strZip = zip;
                strPosition = position;
                strArea = area;
                varDate = date;
            }
    
            public void Save()
            {
              
                
                
                DataRow row = ds.Tables["Employee"].NewRow();
                row["colFName"] = strFName;
                row["colLName"] = strLName;
                row["colPhone"] = strPhone;
                row["colEMail"] = strEMail;
                row["colAddr"] = strAddr;
                row["colCity"] = strCity;
                row["colState"] = strState;
                row["colZip"] = strZip;
                ds.Tables["Employee"].Rows.Add(row);
    
                row = ds.Tables["Manager"].NewRow();
                row["colPosition"] = strPosition;
                row["colArea"] = strArea;
                row["colSDate"] = varDate;
                row["colPhone"] = strPhone;
                ds.Tables["Manager"].Rows.Add(row);
    
                
            }
            
           
        }
    }

  2. #2
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: DataType set to System.String but get error stating System.Int32

    Hi,

    Several of the members of the DateTime structure are Int32 datatypes.
    Look into varDate as the source of your problem

    Curt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured