Jason Kleinhans

Jason Kleinhans

  • NA
  • 7
  • 4.5k

String or binary data would be truncated

Jun 16 2019 2:35 PM
Hi i am new to ASP.NET C# and i am trying to write a simple inventory system , however i receive the following error when i debug the code for errors.
 
System.Data.SqlClient.SqlException: 'String or binary data would be truncated.
The statement has been terminated.'
 
Please be advise that i do know what the error means but i have checked my SQL db table design and the character lengths for each field are set high enough to cater for the text to be entered. see db table design in screenshot below

Here is my C# code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;  
  11. using System.Configuration;  
  12. using System.Security.Cryptography;  
  13. namespace InformationTechnologyInventoryStock  
  14. {  
  15. public partial class Workstations : Form  
  16. {  
  17. private int n;  
  18. public Workstations()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22. private void Workstations_Load(object sender, EventArgs e)  
  23. {  
  24. using (SqlConnection con = new SqlConnection())  
  25. {  
  26. con.ConnectionString = (@"Data Source=SH-JASONK\DEV;Initial Catalog=StockInventory;Integrated Security=True");  
  27. con.Open();  
  28. bool status = false;  
  29. if (combostatus.SelectedIndex == 0)  
  30. {  
  31. status = true;  
  32. }  
  33. else  
  34. {  
  35. status = false;  
  36. }  
  37. using (SqlCommand cmd = con.CreateCommand())  
  38. {  
  39. cmd.CommandText =  
  40. cmd.CommandText = (@"INSERT INTO [StockInventory].[dbo].[Workstations](Emp_Name,  
  41. Emp_Surname, Department, Company,  
  42. Hostname, Wkst_Status, Make, Model, SerialNumber,  
  43. ProductNumber, PurchaseDate, ExpiryDate, Memory,  
  44. Processor, HDD, OS, MSOffice) VALUES ('" + txtname.Text + "','" + txtsurname.Text + "','" + combodept.Text + "','" + combocompany.Text + "','" + txthostname.Text + "','" + combostatus.Text + "','" + combomake.Text + "','" + txtmodel.Text + "','" + textsn.Text + "','" + txtprodnum.Text + "','" + dateTimePicker1.Value.ToString("yyyy/MM/dd") + "','" + dateTimePicker2.Value.ToString("yyyy/MM/dd") + "','" + combomem + "','" + txtproc + "','" + combohdd + "','" + comboOS + "','" + combooffice + "')");  
  45. cmd.ExecuteNonQuery();  
  46. con.Close();  
  47. //Reading Data:  
  48. SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [StockInventory].[dbo].[Workstations] ", con);  
  49. DataTable dt = new DataTable();  
  50. sda.Fill(dt);  
  51. dataGridView1.Rows.Clear();  
  52. foreach (DataRow item in dt.Rows )  
  53. {  
  54. int n = dataGridView1.Rows.Add();  
  55. dataGridView1.Rows[n].Cells[0].Value = item["Emp_Name"].ToString();  
  56. dataGridView1.Rows[n].Cells[1].Value = item["Emp_Surname"].ToString();  
  57. dataGridView1.Rows[n].Cells[2].Value = item["Department"].ToString();  
  58. dataGridView1.Rows[n].Cells[3].Value = item["Company"].ToString();  
  59. dataGridView1.Rows[n].Cells[4].Value = item["Hostname"].ToString();  
  60. if ((bool) item ["Wkst_Status"])  
  61. if (combostatus.SelectedIndex == 0)  
  62. {  
  63. dataGridView1.Rows[n].Cells[5].Value = "ACTIVE";  
  64. }  
  65. else  
  66. {  
  67. dataGridView1.Rows[n].Cells[5].Value = "INACTIVE";  
  68. }  
  69. dataGridView1.Rows[n].Cells[5].Value = item["Make"].ToString();  
  70. dataGridView1.Rows[n].Cells[6].Value = item["Model"].ToString();  
  71. dataGridView1.Rows[n].Cells[7].Value = item["SerialNumber"].ToString();  
  72. dataGridView1.Rows[n].Cells[8].Value = item["ProductNumber"].ToString();  
  73. dataGridView1.Rows[n].Cells[9].Value = item["PurchaseDate"].ToString();  
  74. dataGridView1.Rows[n].Cells[10].Value = item["ExpiryDate"].ToString();  
  75. dataGridView1.Rows[n].Cells[11].Value = item["Memory"].ToString();  
  76. dataGridView1.Rows[n].Cells[12].Value = item["Processor"].ToString();  
  77. dataGridView1.Rows[n].Cells[13].Value = item["HDD"].ToString();  
  78. dataGridView1.Rows[n].Cells[14].Value = item["OS"].ToString();  
  79. dataGridView1.Rows[n].Cells[15].Value = item["MSOffice"].ToString();  
  80. }  
  81. MessageBox.Show("INSERTED SUCCESSFULLY");  
  82. }  
  83. }  
  84. }  
  85. }  

Can you please advise what i am doing wrong or where to search for the issue 
 
thank you in advance
Jason 

Answers (3)