Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Saturday, March 24, 2012

strange error in a very simple method - what is wrong with asp.net?

Hello,

I have a simple method, which looks from my point of view entirely correct, but I
get the error:

;expected (in the line of declaration, CreateDataTable is red underlined )

The code is the following:

private void DataTable CreateDataTable(string selection){
int maxrowindex;

switch (selection)
{
case "newtable" :

DataColumn myDataColumn;
DataRow myDataRow;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "id";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = true;
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "Material";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "Material";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.ColumnName = "Amount";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "Amount";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
myDataTable.Columns.Add(myDataColumn);

DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = myDataTable.Columns["id"];
myDataTable.PrimaryKey = PrimaryKeyColumns;

//myDataSet = new DataSet();
//myDataSet.Tables.Add(myDataTable);

myDataRow = myDataTable.NewRow();
myDataRow["id"] = 1;
myDataRow["Material"] = "Bezeichnung ";
myDataRow["Amount"] = 0;
myDataTable.Rows.Add(myDataRow);
maxrowindex = 1;

break;

case "newrow" :

break;

case "deleterow" :
break;
}

}


Does anybody know what is wrong?void or DataTable, pick one.
thanks!

Strange Error when executing a function

in my code-behind i call a class method that returns a string value...
i run step by step and works great!!

but when i run without breakpoint... sometimes i get error because my string is empty...
and then i run again step by step... and the surprise! works fine again...

why is this happening??

this is my code-behind

string param = " ";

param +=ConfigurarLoja.RetParams( );

Session.Add("ssnHabItemVincPromo",param);

if(param == " ")

{

response.redirect("~/Erro.aspx");

}

And this is my class method:

publicstaticstring RetParams()

{

string param ="";

int cd_empresa =Convert.ToInt32(HttpContext.Current.Session["ssnCdEmpresa"]);

string sql ="SELECT TOP 1 * FROM LjConfig WHERE cd_empresa = " + cd_empresa +" AND ISNULL(DATEADD(dd, 01, dt_exclusao), GETDATE()) > GETDATE() ORDER BY dt_inclusao DESC";SqlConnection con =newSqlConnection(ConfigurationManager.ConnectionStrings["StringDeConexao"].ConnectionString);

con.Open();

SqlCommand cmd =newSqlCommand(sql, con);

SqlDataReader dr = cmd.ExecuteReader();

dr.Read();

if (dr.HasRows)

{

param ="-" + dr["cd_empresa"].ToString().Trim();

}

con.Close();

return param;

}

ops... it seems solved...
i change the sql command 'WHERE'... i think it was a problem with the GETDATE... now its working fine...

AND (ISNULL(DATEADD(dd, 01, dt_exclusao), DATEADD(dd, 01, GETDATE())) > GETDATE()) "

Strange error with SqlDataReader

Hi,
I am using a datareader to fill a datagrid in a usercontrol. I use this method a lot in my application but for an unknown reason, I get this error message in only one web form: "Invalid attempt to FieldCount when reader is closed". What is strange is that the reader is actually open when the error is occuring. The only thing I see that could cause an error is that the user control is created from another usercontrol just after closing another SqlDataReader:

Calling user control


SqlDataReader rdr = cmd.ExecuteReader();
childUC = (DynamicUC)LoadControl( rdr["ASCXDynPage"].ToString() + ".ascx" );
childUC .Language = rdr["NoLanguage"].ToString();
rdr.Close();
childUC.DataBind();

Called user control (DataBind method)

... Creating SQLCommand here ...
SqlDataReader rdr = cmd.ExecuteReader();
gridNews.DataSource = rdr;
gridNews.DataBind();
gridNews.Visible = true;
rdr.Close();

After some tests, the error seam to happen when I set the DataSource of the DataGrid with the DataReader. Also, the error is set at the line where I call the DataBind method of the UserControl childUC. Anyone ever saw that kind of behavior?Maybe your query hasn´t data.
I suggest you use a dataset as datasource of the datagrid.
The query has data.
I tried the dataset, it works. But I really want to know what is wrong with that piece of code... I really don't understand what is going on...

Strange Event Behavior


Scenario:

Class A has a method that returns an instantiated object of Class B.

Class A has a Public Event called "DataUpdated".

When the Class A method is called to return an instance of Class B,
Class A hands to Class B a pointer to itself. Therefore, Class B can
reference the instance of Class A that instantiated it.

Thus, Class B can call the DataUpdated event on the instance of Class A
that instantied it. A method of Class A marked "Friend", called
OnDataUpdated, is called when the "Save" method of Class B is called.

Weirdness:
This all works as planned, but strangely. In an ASP.NET application,
I'm storing an instance of Class A in the Cache. When I call the "Save"
method on Class B, the event bubbles up to the instance of Class A as
expected. However, the event fires up to five times. I've stepped
through the code; the event is only raised once.

Any ideas? Thanks.

Dave Swersky

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!Is it a special needs class?
Try giving them a longer recess. It will allow them to expend some energy
before they return to your classroom.

Tuesday, March 13, 2012

Strange problem

I have a class method which takes integer as parameter...when i call the
method as shown below i get this error message: Specified cast is not valid
<td><%# bs.getPriority((int)DataBinder.Eval(Container.DataItem,
"Priority"))%></td>
When i print the DataItem "Priority" it shows an integer value, but for some
reason the method does not like value.. if i call this method like ->
bs.getPriortiy(3); it works fine.
In the database i have a field called "Priority" int type...
The method works fine when i call it in DataItemBound method..
any idea?Hi,
Try using
int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
DataBinder.Eval returns a string value type. (int) is used to
implicitly cast values to integers. Strings must be explicitly cast
using the Parse method.
Hope this helps.
Tod Birdsall, MCP
http://tod1d.blogspot.com
Tod thank you so much :) it worked..
still why one of them works not the other as shown below:
<TD><%# bs.getCampusName((int)DataBinder.Eval(Container.DataItem,
"CampusID"))%></TD>
<td><%# bs.getCampusName((int.Parse(DataBinder.Eval(Container.DataItem,
"Priority")))%></td>
both CampusID and Priority is same type of value..
Many Thanks again.
"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Sorry that did not solve the problem... :( i am getting this error message
now:
The best overloaded method match for 'int.Parse(string)' has some invalid
arguments
"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Acutally, DataBinder.Eval returns an object unless you pass a third
parameter, a format specifier, i.e.
DataBinder.Eval(Container.DataItem, "Priority", "{0:d}")
Otherwise you get an object reference back, in which case you could
use Convert.ToInt32
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 9 Nov 2004 07:38:03 -0800, "huzz"
<huzz@.discussions.microsoft.com> wrote:
>Sorry that did not solve the problem... :( i am getting this error message
>now:
>The best overloaded method match for 'int.Parse(string)' has some invalid
>arguments
>"Tod Birdsall" wrote:
>
Hi,
Scott is correct. Sorry for the mis-information.
I tried to reproduce your error message. I created a function in the
code behind like so:
protected string GetRemainderInEscrow(decimal approvedAmount, object
paidAmount)
I then called the method from the aspx page using the following code :
<%# GetRemainderInEscrow( (decimal)DataBinder.Eval(Container,
"DataItem.ApprovedAmount"), DataBinder.Eval(Container, "DataItem.Paid")
) %>
The code worked as it should. If you have control over the
'bs.getPriority' method, try allowing an object to be passed instead of
an integer value type. If that doesn't work. If that still does not
work, copy and paste the exact error message and Stack Trace.
Tod Birdsall, MCP
http://tod1d.blogspot.com

Strange problem

I have a class method which takes integer as parameter...when i call the
method as shown below i get this error message: Specified cast is not valid

<td><%# bs.getPriority((int)DataBinder.Eval(Container.Data Item,
"Priority"))%></td
When i print the DataItem "Priority" it shows an integer value, but for some
reason the method does not like value.. if i call this method like ->
bs.getPriortiy(3); it works fine.

In the database i have a field called "Priority" int type...

The method works fine when i call it in DataItemBound method..

any idea?Hi,

Try using

int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))

DataBinder.Eval returns a string value type. (int) is used to
implicitly cast values to integers. Strings must be explicitly cast
using the Parse method.
Hope this helps.

Tod Birdsall, MCP
http://tod1d.blogspot.com
Tod thank you so much :) it worked..

still confused why one of them works not the other as shown below:
<TD><%# bs.getCampusName((int)DataBinder.Eval(Container.Da taItem,
"CampusID"))%></TD
<td><%# bs.getCampusName((int.Parse(DataBinder.Eval(Contai ner.DataItem,
"Priority")))%></td
both CampusID and Priority is same type of value..

Many Thanks again.

"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Sorry that did not solve the problem... :( i am getting this error message
now:

The best overloaded method match for 'int.Parse(string)' has some invalid
arguments

"Tod Birdsall" wrote:

> Hi,
> Try using
> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
> DataBinder.Eval returns a string value type. (int) is used to
> implicitly cast values to integers. Strings must be explicitly cast
> using the Parse method.
> Hope this helps.
> Tod Birdsall, MCP
> http://tod1d.blogspot.com
>
Acutally, DataBinder.Eval returns an object unless you pass a third
parameter, a format specifier, i.e.

DataBinder.Eval(Container.DataItem, "Priority", "{0:d}")

Otherwise you get an object reference back, in which case you could
use Convert.ToInt32

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 9 Nov 2004 07:38:03 -0800, "huzz"
<huzz@.discussions.microsoft.com> wrote:

>Sorry that did not solve the problem... :( i am getting this error message
>now:
>The best overloaded method match for 'int.Parse(string)' has some invalid
>arguments
>"Tod Birdsall" wrote:
>> Hi,
>>
>> Try using
>>
>> int.Parse(DataBinder.Eval(Container.DataItem, "Priority"))
>>
>> DataBinder.Eval returns a string value type. (int) is used to
>> implicitly cast values to integers. Strings must be explicitly cast
>> using the Parse method.
>> Hope this helps.
>>
>> Tod Birdsall, MCP
>> http://tod1d.blogspot.com
>>
>
Hi,

Scott is correct. Sorry for the mis-information.

I tried to reproduce your error message. I created a function in the
code behind like so:

protected string GetRemainderInEscrow(decimal approvedAmount, object
paidAmount)

I then called the method from the aspx page using the following code :

<%# GetRemainderInEscrow( (decimal)DataBinder.Eval(Container,
"DataItem.ApprovedAmount"), DataBinder.Eval(Container, "DataItem.Paid")
) %
The code worked as it should. If you have control over the
'bs.getPriority' method, try allowing an object to be passed instead of
an integer value type. If that doesn't work. If that still does not
work, copy and paste the exact error message and Stack Trace.
Tod Birdsall, MCP
http://tod1d.blogspot.com