I have a page with a nested Gridview where I'm trying to fill the inner grid by pulling a string value from each line of the outer grid. When I try to pass the value from the outer grid to the string variable I get the "Error when converting 'String' to 'System.IFormatProvider'" error. I'm using the following code to store the gridview cell value to the variable:
Dim Svc_Name As String = grdOuterGridView.DataKeyNames(e.Row.RowIndex).ToString("THIRD_PARTY_SERVICE")
Can anyone tell me what I'm doing wrong here? Thanks.
The DataKeysNames is already an array of string, you don't need the ToString(....) part.
Dim Svc_Name As String = grdOuterGridView.DataKeyNames(e.Row.RowIndex)
Actually your error comes from the ToString("THIRD_PARTY_SERVICE"). The ToString() override that takes one parameter requires an object that implements IFormatProvider interface, but of course a string doesn't have this interface, thus the error. However, calling ToString() on a String has no effect as you can read from the MSDN docs
Returns this instance of String; no actual conversion is performed.
Related
How to write the following code so that it will not return the error object reference not set ....
Below is the code.
Private Quantity As String
Public Property Quantity1() As String
Get
Return Quantity.ToString()
End Get
Set(ByVal value As String)
Quantity = value
End Set
End Property
Instantiate your variables. For example...
Private Quantity As String = ""
On another note, you do not have to add ToString to your strings as they are already Strings, it's a non-needed cast.
First, remove the .ToString() calls from your properties. They are completely redundant since the variables you are returning are already strings.
Second, Strings in .net are reference types. Therefore, when you write something like Private Quantity As String, the Quantity is null (Nothing in vb.net), since it points to no string. If you wrote Private Quantity As String = String.Empty then it will not be null.
May be you needs to initialize your private vars with an initial value, or where are you assigning any value to them?
Private Quantity As String = ""
Is it possible to cast a command-line passed string object back to actual object?
I want to do the following, but throwing error can't cast.
Button objPro = (Button) sender;
cProduct cp = (cProduct) objPro.CommandArgument;
If no, then why?
This is what the string holds.
cProduct cpObj = (cProduct)e.Row.DataItem;
Button btnAddProduct = (Button)e.Row.FindControl("btnAddProduct");
if (btnAddProduct != null)
{
btnAddProduct.CommandArgument = cpObj.ToString();
}
You probably can't, because it's a string. It's not a cProduct (whatever that is - consider following .NET naming conventions and naming it Product instead).
Now you could do this if you had a explicit conversion operator in cProduct to create an instance from a string.
You haven't really explained what's in the string, or what's in the type - but if your cProduct type provides a ToString method which contains all the data in a reversible form, then you could easily write a method or a constructor to create the product again:
Product product = new Product(objPro.CommandArgument);
or maybe:
Product product = Product.Parse(objPro.CommandArgument);
You'll have to write that constructor/method, of course.
I would strongly recommend using a constructor or method instead of an operator, just to keep your code clearer - it's very rarely a good idea to write your own conversion operators.
Take a look at CommandArgument on MSDN. The property is a string, when you assign the a value to the property, you aren't casting some complex type to string, you are setting a string value on the property. Can you cast a string back to your object type anyway, regardless of it being a CommandArgument. I doubt it. If the argument is an int you could try int.Parse or similar for other types which have a parse method.
I need to assign my variable to session. I tried this:
string name = string.Empty
Session["N"] = name;
and it won't work.
Error 1 Invalid token '[' in class, struct, or interface member declaration
Error 2 Invalid token '"N"' in class, struct, or interface member declaration
Error 3 Identifier expected
Where I'm wrong?
I'm using ASP.net in Visual Studio 2008.
Error 1 Invalid token '[' in class, struct, or interface member declaration
Error 2 Invalid token '"N"' in class, struct, or interface member declaration
Error 3 Identifier expected
missing ; end of first line.
string name = string.Empty;
Session["N"] = name;
string test= Session["N"].ToString();//Catch Your session
There is nothing wrong with that code (except a missing semicolon, as Shree Khanal pointed out, but that can't be the issue, right?).
As long as the code is in the page class, the Session property is available. If you have the code in a different class, you don't have the Session property available, then you need to get it from the current context:
HttpContext.Current.Session["N"] = name;
When reading the value from the session collection, the type is Object, not String, so you need to cast it:
string name = Session["N"] as string;
Using the as keyword means that you can attempt to read the value even if it would not exist, or if it happens to be set to a different data type. In that case you will get a null reference back.
DetailsView is bound to ObjectDataSource. Inside Detailsview’s EditItemTemplate are two TextBoxes ( T1 and T2 ). T1 is mapped to update parameter of type String, while T2 is mapped to update parameter of type DateTime.
Assuming both TextBoxes contain an empty string, then when I try to update the data source by clicking on DetailsView’s Update button, ODS ( or is it perhaps DetailsView ) automatically converts T1’s empty string to null, while T2’s empty string doesn’t get converted to null. I’ve tried to prevent ODS from converting T1’s empty string to null by setting T1’s update parameter’s ConvertEmptyStringToNull property to false ( I ‘ve also set <asp:TemplateField ConvertEmptyStringToNull=”false” …>, but to no effect.
a)Any idea why T1’s empty string gets converted, while T2’s doesn’t?
b) Also, how can I prevent the conversion( BTW - I realize I could convert null back to empty string inside update method )?
thanx
a)Any idea why T1’s empty string gets
converted, while T2’s doesn’t?
T2 is a DateTime which is a value type. Value types can't be null. Well unless you use the Nullable type
b) Also, how can I prevent the
conversion( BTW - I realize I could
convert null back to empty string
inside update method )?
EDIT: I've tried to duplicate the problem above, but I could only duplicate the problem when I didn't specify ConvertEmptyStringToNull="false" in the <asp:TemplateField> of the bound control AND the <asp:Parameter> of the <asp:ObjectDataSource>. If you leave either out then you will get the null value on an empty field. With the ConvertEmptyStringToNull="false" defined in both places it does not convert the empty string to a null value. The empty string is passed correctly. You said that you did try it in both places, so I'm not sure why it's not working for you. Maybe you could show us your datasource and detailsview markup.
With that said I think it is still a good idea to make the check described below in your business class. Like you said you can convert null back to an empty string. This is how I have done it:
I have a helper class, lets call it BizObject, that contains this method:
protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}
Then in my business class's Insert/Update method I call ConvertNullToEmptyString on each string parameter:
public static bool UpdateSource(string sourceName, DateTime sourceDate)
{
sourceName = BizObject.ConvertNullToEmptyString(sourceName);
...
bool ret = UpdateSource(record);
return ret;
}
Just to make this clear - what is the difference between:
String(value)
and
value as String
What are the cases where you would use one over the other? They seem interchangeable...
Casting with Type(variable) can cause a runtime exeception (RTE), while "variable as type" will return null instead of throwing an exception.
See http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/ for more explanations.
String (value) creates a new String object from a string literal. If the constructor argument is not a string literal, I assume it calls the argument object's .toString() method.
value as String will simply pass back value IF value is a String or a subclass of String. It will pass back null if value is not of type String.
The important thing to note is that String(val) creates a new object whereas value as String simply refers to value (and tests for compatibility to String).
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#as