when i tried to insert some data into the SQL Server there was these extra characters
for example my textBox1.text == "SAMPLE"
then this was the code
string qry = "insert into FinalImages (FinalImageName, Date) values(@FinalImageName, @Date)";
SqlConnection c = new SqlConnection(c_string);
//Initialize SqlCommand object for insert
SqlCommand SqlCom = new SqlCommand(qry, c);
SqlCom.Parameters.Add(new SqlParameter("@FinalImageName", SqlDbType.Char, 40)).Value = textBox1.Text;
SqlCom.Parameters.Add(new SqlParameter("@Date", SqlDbType.Char, 40)).Value = DateTime.Now.ToString("MM/dd/yyyy");
but when i check in the tables the record was this "SAMPLE                             ", it has 34 space characters added
what should be wrong?
SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.
What exactly is 'white space'? White space is a character in a string that represents horizontal or vertical space in typography. In other words: tabs, line feeds, carriage returns and, yes, spaces. A white space has character value, just like 'A', 'B' and 'C' have a value.
Takeaway: According to SQL Server, an identifier with trailing spaces is considered equivalent to the same identifier with those spaces removed.
Don't use SqlDbType.Char, use NVarChar. Char is a fixed-size string, padded with spaces (a terrible monster).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With