Domanda

I got this OfficeWriter error when debugging the console application. I used methods to retrieve config details for the database used in the coding from the master database, and ended up having this error.

Error binding in GetColumnNumber at row 1

Attached here is partial coding for my work. Anyone can explain me what the error is?

    SqlDataReader rdSource = getSource();
    SqlDataReader rdDestination = getDestination();
    SqlDataReader rdCode = getCode();

    while (rdSource.Read() && rdDestination.Read())
    {
        string src = rdSource["Value"].ToString();
        string dest = rdDest["Value"].ToString();

        ExcelTemplate XLT = new ExcelTemplate();
        XLT.Open(src);
        DataBindingProperties dataProps = XLT.CreateBindingProperties();
        XLT.BindData(rdCode, "Code", dataProps);
        XLT.Process();
        XLT.Save(dest);
    }

    //rdCode method
    SqlDataReader rdConnection = getConnection(); //method for getting connection from master
    while (rdConnection.Read())
    {
        string con = rdConnection["Value"].ToString();
        SqlConnection sqlCon = new SqlConnection(con);

        string SQL = "SELECT * FROM Sales.Currency";
        sqlCon.Open();
        SqlCommand cmd = new SqlCommand(SQL, sqlCon);
        cmd.ExecuteReader();
        sqlCon.Close();
    }
    return rdConnection;

    //getConnection method
    string strCon = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
    SqlConnection sqlCon = new SqlConnection(strCon);
    string cSQL = "SELECT Value FROM dbo.COMMON_CONFIG WHERE Value = 'Data Source=localhost;Initial Catalog=Test;Integrated Security=True'";
    SqlCommand cmd = new SqlCommand(cSQL, sqlCon);
    sqlCon.Open();
    return new SqlCommand(cSQL, sqlCon).ExecuteReader(CommandBehavior.ConnectionString);

    //getSource & getDestination methods
    string strCon = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
    SqlConnection sqlCon = new SqlConnection(strCon);
    string srcPath = @"FILE PATH NAME"; //change to destPath for getDestination
    string sSQL = "SELECT Value FROM dbo.COMMON_CONFIG WHERE Value = '" + srcPath + "'";
    SqlCommand cmd = new SqlCommand(cSQL, sqlCon);
    sqlCon.Open();
    return new SqlCommand(cSQL, sqlCon).ExecuteReader(CommandBehavior.ConnectionString);
È stato utile?

Soluzione

The error is a generic message that is thrown by ExcelWriter when it is unable to bind data to the template.

I think this might be caused by your getCode() method. In getCode(), you use a SQLDataReader to retrieve the connection string for the database:

    SqlDataReader rdConnection = getConnection(); //method for getting connection from master

Then you execute a SQL query against that database, but you don't actually get a handle on the SqlDataReader that is executing the SQL query to return the data.

    SqlCommand cmd = new SqlCommand(SQL, sqlCon);
    cmd.ExecuteReader(); //Note: This returns the SqlDataReader that contains the data

Then you return rdConnection, which is the SQLDataReader for the connection string - not the data you are trying to import. rdConnection contained 1 row and you already called Read(), so there are no records left to read.

 SqlDataReader rdCode = getCode();
 ...
 XLT.BindData(rdCode, "Code", dataProps);

The SQL reader you are binding is the used 'connection string', rather than your sales data. I would recommend the following:

  1. Return the new SqlDataReader that is generated by cmd.ExecuteReader() in getCode(), rather than rdConnection.
  2. Do not close the connection to this new SqlDataReader. ExcelWriter needs to be able to read the data reader in order to bind the data. If you close the connection, ExcelWriter will not be able to bind data correctly.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top