Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
string fname = openFileDialog1.FileName;
           
TextReader trs = new StreamReader(@fname);
String s=trs.ReadLine();
s=s.ToString();
while (s != null)
{
  String x = "";

  for (int i = 0; i < s.Length; i++)
  {

    if(s[i]!='.')//add the required special characters along withit
    x += s[i];
  }
  s = x;
  s = trs.ReadLine();
  conn.Open();
  SqlCommand cmd = new SqlCommand("insert into C_word (Words,Category) values('" + s +"','" + comboBox1.SelectedText + "')",conn);
  cmd.ExecuteNonQuery();
  conn.Close();

}


While using this code I got this error:
String or binary data would be truncated.
The statement has been terminated.

Please, can anyone clear it?
Posted
Updated 26-Oct-10 3:43am
v2
Comments
Goutam Patra 26-Oct-10 9:43am    
What you are trying to do? Reading a Word document as text?
Abhinav S 26-Oct-10 12:05pm    
Goutam - The OP is reading a word document as text and then inserting this text into a database.
Crushe 11-May-16 5:27am    
If you want to open and read Word documents in C#, you cannot just use StreamReader because Word documents are not plain text format, you'll need to change your approach.
For example you could use this C# library for word that can read your documents and export their content into a string.
However if you want to store the document into a database then why not store it into a VARBINARY field, not VARCHAR. In this case instead of StreamReader you would use File.ReadAllBytes.

Just a guess, but I'd say either the Word field or Category field is defined as a limited length string and is too short for the data you're trying to stuff into it.
 
Share this answer
 
Comments
Abhinav S 26-Oct-10 12:05pm    
Yup my 5. That is probably what it is.
Increase the size of the varchar field in your database.
See here[^].
 
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