Add Table By code

Stimulsoft Reports.NET discussion
Post Reply
raed
Posts: 4
Joined: Thu Apr 25, 2013 4:36 pm

Add Table By code

Post by raed »

I downloaded trial version 2013.1 to try your product.
I tried to create report using c# code the problem that I cannot add Stitable object because it raise the following exception:
Index was out of range. Must be non-negative and less than the size of the collection

I use the following code
StiReport report = new StiReport();
StiPage page = report.Pages[0];

StiTable Table1 = new StiTable();

page.Components.Add(Table1);
report.Compile();
report.Show();

Notes:
When I tried to add databand or any other object it is working fine
I tired to add the datasource and cells but the same
If I save the report using previous code then I open it using the designer its working fine even If I open it using VS.

thanx
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Add Table By code

Post by Alex K. »

Hello,

Please try to add the necessary component in designer then open Code tab and see how to create this components. Then you can use the similar code in your project.

Thank you.
raed
Posts: 4
Joined: Thu Apr 25, 2013 4:36 pm

Re: Add Table By code

Post by raed »

Thank u very much it is working
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Add Table By code

Post by Alex K. »

Hello,

We are always glad to help you!

Thank you.
raed
Posts: 4
Joined: Thu Apr 25, 2013 4:36 pm

Re: Add Table By code

Post by raed »

I tried to add the datasource to the report from code
I read one of ur replies on the forum how to do that :

report.Dictionary.Databases.Clear();
report.Dictionary.Databases.Add(new Stimulsoft.Report.Dictionary.StiSqlDatabase("Connection", newConnectionString));

StiSqlSource DS1 = new StiSqlSource("Connection", "DS1", "DS1", "SELECT * FROM DS1", true, false);
report.Dictionary.DataSources.Add(DS1);


Code: Select allforeach (DataColumn col in dataTableDS1.Columns)
{
DS1.Columns.Add(col.ColumnName, col.DataType);
}

But the problem that the datasource created without columns bcz I don't know what u mean by dataTableDS1.Columns
in ur last code how to add the columns to the data source (how to define the data table dataTableDS1)

thanx
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Add Table By code

Post by Alex K. »

Hello,

Please check the following code:

Code: Select all

StiReport report = new StiReport();

string newConnectionString = @"Data Source=ALEKSEYPC\SE2012;Initial Catalog=Northwind;Integrated Security=True";
report.Dictionary.Databases.Clear();

report.Dictionary.Databases.Add(new Stimulsoft.Report.Dictionary.StiSqlDatabase("Connection", newConnectionString));

StiSqlSource Categories = new StiSqlSource("Connection", "Categories", "Categories", "SELECT * FROM Categories", true, false);
report.Dictionary.DataSources.Add(Categories);

using (SqlConnection connection = new SqlConnection(newConnectionString))
{
    SqlDataAdapter dataCategories = new SqlDataAdapter("select * from Categories", connection);
    DataTable dataTableCategories = new DataTable();
    dataCategories.Fill(dataTableCategories);

    foreach (DataColumn col in dataTableCategories.Columns)
    {
        Categories.Columns.Add(col.ColumnName, col.DataType);
    }
}
Thank you.
raed
Posts: 4
Joined: Thu Apr 25, 2013 4:36 pm

Re: Add Table By code

Post by raed »

Thank u very much
Andrew
Posts: 4109
Joined: Fri Jun 09, 2006 3:58 am

Re: Add Table By code

Post by Andrew »

Hello,

We are glad to help you!
Post Reply