简体   繁体   中英

Erasing blank spaces from columns in a database

I have a datagridview that shows one table from my sql but it has some blankspaces as you can see in this screenshot:

在此处输入图片说明

And I was wondering if it's possible to remove those blank spaces. I have already tried this:

SqlCommand objcmd1 = new SqlCommand("UPDATE dbo.UXMenu SET TransDocument = 
RTRIM(TransDocument) WHERE Estado = 0", myConnection);
objcmd1.ExecuteNonQuery();

But instead of removing the blank spaces it removes the entire row.

Here is my code for display the data in the datagridview, if you want more details about the code feel free to ask.

InitializeComponent();
SqlConnection myConnection = new SqlConnection(@"Data source = ****;Database=****; User Id=****; Password=****");
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT TransDocument, TransSerial, 
TransDocNumber, PartyName, PartyLocalityID, TotalAmount, ShipToPostalCode FROM dbo.UXMenu WHERE Estado = 0", myConnection);
objcmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.AutoSizeColumnsMode = 
DataGridViewAutoSizeColumnsMode.DisplayedCells;

UPDATE 1:

I added the code that I use to create the table

SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(TransDocument char(5), TransSerial char(5), TransDocNumber float  PRIMARY KEY, PartyName char(60), PartyLocalityID char(5), TotalAmount char (25), ShipToPostalCode char(35), Estado int);", myConnection);

UPDATE 2:

Added the code that creates the textfile

if (dataGridViewEnviarDados.SelectedRows.Count > 0)
{
 foreach (DataGridViewRow r in dataGridViewEnviarDados.SelectedRows)
  {
   var transactionHandler = new TransactionHandler();
   FileInfo arquivo = new FileInfo(@"C:\Users\HP8200\Desktop\faturas\" + r.Cells[0].Value.ToString() + r.Cells[1].Value.ToString() + r.Cells[2].Value.ToString() + ".txt");                    
     using (TextWriter tw = new StreamWriter(arquivo.FullName, false, Encoding.Default))
    {
      foreach (DataGridViewColumn c in dataGridViewEnviarDados.Columns)
    {
     tw.Write(r.Cells[c.Name].Value.ToString() + ";");
    }
    tw.Close();
  }
 }
}

UPDATE 3:

Changed my code to this:

SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(TransDocument varchar(5), TransSerial varchar(5), TransDocNumber float  PRIMARY KEY, PartyName varchar(60), PartyLocalityID varchar(5), TotalAmount varchar(25), ShipToPostalCode varchar(35), Estado int);", conn);

And now my table look like this:

在此处输入图片说明

You are using char datatype - this will create a column that is always that length, so by doing char(35) you are saying "create me a column that is 35 chars wide, even if i only put 2 characters in it".

You probably want varchar which is a variable character length field.

I needed to change my chars to varchar and did something like this:

SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS
 NULL CREATE TABLE UXMenu(TransDocument varchar(5), TransSerial
 varchar(5), TransDocNumber float  PRIMARY KEY, PartyName varchar(60),
 PartyLocalityID varchar(5), TotalAmount varchar(25), ShipToPostalCode
 varchar(35), Estado int);", conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM