TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Security.Cryptography;
namespace
InformationTechnologyInventoryStock
{
public
partial
class
Workstations : Form
{
private
int
n;
public
Workstations()
{
InitializeComponent();
}
private
void
Workstations_Load(
object
sender, EventArgs e)
{
using
(SqlConnection con =
new
SqlConnection())
{
con.ConnectionString = (@
"Data Source=SH-JASONK\DEV;Initial Catalog=StockInventory;Integrated Security=True"
);
con.Open();
bool
status =
false
;
if
(combostatus.SelectedIndex == 0)
{
status =
true
;
}
else
{
status =
false
;
}
using
(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText =
cmd.CommandText = (@"INSERT INTO [StockInventory].[dbo].[Workstations](Emp_Name,
Emp_Surname, Department, Company,
Hostname, Wkst_Status, Make, Model, SerialNumber,
ProductNumber, PurchaseDate, ExpiryDate, Memory,
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 + "'
)");
cmd.ExecuteNonQuery();
con.Close();
//Reading Data:
SqlDataAdapter sda =
new
SqlDataAdapter(
"SELECT * FROM [StockInventory].[dbo].[Workstations] "
, con);
DataTable dt =
new
DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach
(DataRow item
in
dt.Rows )
{
int
n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item[
"Emp_Name"
].ToString();
dataGridView1.Rows[n].Cells[1].Value = item[
"Emp_Surname"
].ToString();
dataGridView1.Rows[n].Cells[2].Value = item[
"Department"
].ToString();
dataGridView1.Rows[n].Cells[3].Value = item[
"Company"
].ToString();
dataGridView1.Rows[n].Cells[4].Value = item[
"Hostname"
].ToString();
if
((
bool
) item [
"Wkst_Status"
])
if
(combostatus.SelectedIndex == 0)
{
dataGridView1.Rows[n].Cells[5].Value =
"ACTIVE"
;
}
else
{
dataGridView1.Rows[n].Cells[5].Value =
"INACTIVE"
;
}
dataGridView1.Rows[n].Cells[5].Value = item[
"Make"
].ToString();
dataGridView1.Rows[n].Cells[6].Value = item[
"Model"
].ToString();
dataGridView1.Rows[n].Cells[7].Value = item[
"SerialNumber"
].ToString();
dataGridView1.Rows[n].Cells[8].Value = item[
"ProductNumber"
].ToString();
dataGridView1.Rows[n].Cells[9].Value = item[
"PurchaseDate"
].ToString();
dataGridView1.Rows[n].Cells[10].Value = item[
"ExpiryDate"
].ToString();
dataGridView1.Rows[n].Cells[11].Value = item[
"Memory"
].ToString();
dataGridView1.Rows[n].Cells[12].Value = item[
"Processor"
].ToString();
dataGridView1.Rows[n].Cells[13].Value = item[
"HDD"
].ToString();
dataGridView1.Rows[n].Cells[14].Value = item[
"OS"
].ToString();
dataGridView1.Rows[n].Cells[15].Value = item[
"MSOffice"
].ToString();
}
MessageBox.Show(
"INSERTED SUCCESSFULLY"
);
}
}
}
}
}
Can you please advise what i am doing wrong or where to search for the issue
thank you in advance
Jason
Reply
Answers (
3
)
Sir i have one error on GYM project
What are the Best Approaches?