stored procedure expects parameter which was not supplied - asp.net

I'm calling a stored procedure from C# .net and one of the string parameters is null. When that is the case, I pass DBNull.Value. However, I get the above error. Any ideas?

If the string is null, you will see this error. To avoid it, you can set a default parameter value in your stored proc, or you can pass DBNull.Value if your string is null.

You get this if the value of a parameter is "null" (as opposed to DBNull.Value).
Are you sure the parameter value is DBNull.Value?

Do you have access to the Stored Procedure? If so, (And if the Stored procedure logic will allow it), modify the declaration of the input parameter to add " = Null" at the end, as in
Create procedure ProcName
#MyParameterName Integer = Null,
-- con't
As...

Can you give more details like how you are calling the sproc, the parameter itself and the value. You know in your sproc you can set default values for variables.
Something to the effect of:
ALTER SprocMySproc
#myvar varchar(50)=NULL
SELECT blah FROM MyTable WHERE MyField=#myvar OR #myvar IS NULL
Your actual C# or vb.net code can then ignore sending the parameter if it is null or empty
if(!(String.IsNullOrEmpty(myVar)))
{
//pass the parameter
mySQLCommandObject.Parameters.Add("#myvar", sqldbtype.varchar).Value = myVar;
//other code...
}

Related

How to handle identity fields in GraphQL SPQR

Let's say I have
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="trade_id")
int tradeId;
When I query this from the database, I will want to get the tradeId. However, when performing a Create, obviously I won't have the tradeId, since the Database is going to generate it when I insert it.
input TradeInput {
tradeId: Int!
...
But the schema SPQR is generating for me is setting this field to Not Null, however.
So my question is, how can prevent this field from automatically coming up as Not Null, so I won't have to send it down on Creates, but can retrieve it.
An int simply can not be null. But you have a few options:
Turn it into an ID scalar type (using #GraphQLId) instead of an Int
Give it a default value via #GraphQLInputField
Use Integer instead of int, as Integer is nullable

Symbol was not found when using tmpaccountsum variable in form

In the ReqTransPO form I want to use the tmpAccountSum table to store some values in which I later need to filter my grid.
In the classdeclaration I declared:
tmpAccountSum mytable;
In a helper method I have:
//Store data in tmptable
ttsbegin;
mytable.AccountNum = _reqTrans.RefId;
mytable.Txt = _reqPo.RefId;
mytable.Voucher = enum2str(_prodstatus);
mytable.insert();
ttscommit;
I don't get an error on 'mytable' but can't really use it / it's not initialized. If I add a watch in debug I see:
Error: Symbol "mytable" was not found
If I declare mytable locally it works ok.
Guess I'm doing something wrong?
Thanks in advance,
Mike
My extrasensory perception tells me, that your helper method is static.
Static methods do not have access to instance variables declared in classDeclaration.

Error when converting 'String' to 'System.IFormatProvider'

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.

Can I cast a string object passed on command line argument to the actual object?

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.

ConvertEmptyStringToNull=”false” and yet the conversion still happens

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;
}

Resources