Question

Hello I'm trying to edit my klientTableAdapter.CommanText which is TableAdapter in dynamically created DataSet called DSSchema - this was dynamically created when creating report using ReportViewer.

string sqlcomEvent = "SELECT akce,name,surname,rodcis,(Cast([street] as nvarchar(MAX)) + ', '+ city + ', ' + ZIP) AS Adresa, odjezd, sitting, rocnik FROM dbo.klient WHERE (event=@event";

for (int a = 0; a < CommonEvents.Count(); a++)
{
    sqlcomEvent += " OR event='" + CommonEvents[a]+"'";
}
sqlcomEvent += " )AND year=@year ORDER BY SITTING";
// TODO: This line of code loads data into the 'dtourDataSet.klient' table. You can move, or remove it, as needed.
this.klientTableAdapter.Adapter.SelectCommand.CommandText = sqlcomEvent ;            
this.klientTableAdapter.Fill(this.dtourDataSet.klient,zajezd,klientClass.Rocnik());
this.reportViewer1.RefreshReport();

I believe the NullReferenceException is raised because I havent declared klientTableAdapter before, but when I add this line var klientTableAdapter = new klientTableAdapter(); I get error:

tours.TiskSchemaDoprava.klientTableAdapter is a field; but is used like a type

Would anyone please help me solve this out ?

This works perfectly for me but Iam not sure if there isn't smarter way to do so?

this.klientTableAdapter.Fill(this.dtourDataSet.klient,zajezd,klientClass.Rocnik());
this.klientTableAdapter.Adapter.SelectCommand.CommandText = sqlcomZaj;
this.klientTableAdapter.Fill(this.dtourDataSet.klient, zajezd, klientClass.Rocnik());
this.reportViewer1.RefreshReport();
Was it helpful?

Solution

You have a field:

this.klientTableAdapter

It's type has the same name as the field name. So when you say new klientTableAdapter() it doesn't know whether you are referring to the type of to the field, and as the field is defined in the class it is picking the field: and you can't 'new' a field. The easiest thing to do is to rename the field, and let Visual Studio take care of renaming it wherever it's used. Or do that to the type and give a standard name with a capital at the front KlientTableAdapter so the compiler can disambiguate the field from its type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top