Arun Kumar
Arun Kumar

Reputation: 877

MaxLength of column seems to be always -1 for the string fields in the datatable C#

All my data table column fields from the stored procedure is string fields as like given below

    [Account Code] varchar(10),
    Filler1 varchar(5), 
    [Accounting Period] varchar(7),
    [Transaction Date] varchar(8),
    Filler2 varchar(2),
    [Record Type] varchar(1),
    [Source] varchar(2),
    [Journal No] varchar(5),
    [Journal Line] varchar(7)

Using the below code only I am picking the value from the SP to the dataset as given below

public DataSet InvoicesToSageExtractGoodsIn(string invoiceDateFrom)
{

    SqlConnection conn = null;
    DALFactory dalFactory = null;
    SqlDataAdapter da = null;

    try
    {
        DataSet dsInvoiceCustomer = new DataSet();
        dalFactory = new DALFactory();

        conn = new SqlConnection(dalFactory.ConnectionStringVI);
        conn.Open();

        SqlCommand sqlCommand = new SqlCommand("prc_ReturnSageExtractGoodsIn", conn);
        sqlCommand.CommandType = CommandType.StoredProcedure;
        DALSystemTable dst = new DALSystemTable(this.dao);
        sqlCommand.CommandTimeout = Convert.ToInt32(dst.GetSystemConstant("CommandTimeOut"));

        SqlParameter param1 = sqlCommand.Parameters.Add("@InvoiceDateFrom", SqlDbType.DateTime);//VI-165
        param1.Direction = ParameterDirection.Input;
        param1.Value = invoiceDateFrom;                               

        DataSet dsInvoice = new DataSet("VisionInvoicing");
        DataTable dtInvoiceNos = new DataTable("tblSageExtractGoodsIn");

        da = new SqlDataAdapter(sqlCommand);
        da.Fill(dsInvoice);

        dsInvoice.Tables[0].TableName = "tblGenerateInvoice";


        return dsInvoice;
    }
    catch (Exception ex)
    {
        throw new DatabaseException(ex.Message, ex);
    }

}

When I try to pick the MaxLength of the column (dtSAGEExtract.Columns[i].MaxLength) from that dataset table it always shows -1 value.

foreach (DataRow drExtract in dtSAGEExtract.Rows)
{
    int fieldCount = (dtSAGEExtract.Columns.Count);

    for (int i = 0; i < fieldCount; i++)
    {
        int length = dtSAGEExtract.Columns[i].MaxLength;
        writer.Write(drExtract[i].ToString().PadRight(length));
    }

    writer.WriteLine();
}

writer.Close();

Does anyone have any idea why it is always showing -1 for all the fields?

Upvotes: 3

Views: 9593

Answers (3)

t3chb0t
t3chb0t

Reputation: 18655

First you need to add your DataTable to the DataSet to make it work after you created the DataTable because they are not connected in your example and filling the DataSource has no impact on the DataTable created in the next line so that you can use the additional schema information:

dsInvoice.Tables.Add(dtInvoiceNos);

then you need to call the SqlDataAdapter.FillSchema Method on your SqlDataAdapter to get more information from your database then just the data when using only the Fill method

da.FillSchema(dtInvoiceNos, SchemaType.Source);
da.Fill(dsInvoice);

The fill operations must be called after you added the table to the dataset.

Upvotes: 4

user2024503
user2024503

Reputation: 44

Because you not fill schema to dataset.

Normaly -1 is default length of column.

You should use:

da.FillSchema(dsInvoice, SchemaType.Mapped)

UPDATE:

DataTable dtInvoiceNos = dsInvoice.Tables[0];

Please check: http://msdn.microsoft.com/en-us/library/229sz0y5(v=vs.110).aspx

Upvotes: 0

dotnetstep
dotnetstep

Reputation: 17485

It does not represent the MaxLength of DataTable column. It is not depend on database schema either. It return -1 because it is default value when you do not set anything.

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.maxlength(v=vs.110).aspx

Upvotes: 1

Related Questions