I have a textbox in my .rdlc page which gets values from sql server. However, i want to add a default value to the textbox if the value is NULL. Currently, it shows nothing when the value from sql is NULL.
Two ways :-
Do it in the DataSource itself as mentioned by mason in the other answer. If you are using SQL Server, ISNULL() function will help.
You can use the isnothing() function. Ex iif(isnothing(Field!FieldName.Value),"Default Value",Field!FieldName.Value).
Use the ISNULL function.
select username, ISNULL(firstname, "no name") from users
Related
I am using ASP.NET Web Forms. I am trying to bind some data from a previous page (including the URL and some specific figures stored in the URL) to session variables for use in a SQL insert statement to a SQL Server database.
An example of one of the assignments is below:
this.Session["URL"] = HttpUtility.ParseQueryString(Request.UrlReferrer.Query)["helpurl"];
The problem I am having is that on executing the SQL insert, the value does not seem to be present and the default value is recorded. SessionState is enabled. I am able to print the result of the HttpUtility method to a label, and I am also able to assign to session variables for a SQL statement if I assign them directly, eg:
this.session["Variable"] = Variable;
Any ideas why the result of the HttpUtility function may not be assigned to the session variable prior to the execution of the SQL insert statement?
It's ok - I placed the assignments inside an
if(!IsPostBack)
And they now work ok and are being recorded to the database. I also removed the 'this' keyword from them.
Thanks anyway
I have done IN statement straight to SQL but I was trying to use it the objecdatasource. I have a textbox where users need query multiple item codes like this 1001,1002,1003 but how can I use this IN statement with a parameter with objectdatasource?
TIA
Just pass the textbox value of "1,2,3" to the parameter as a full string, and split the contents by "," within the object your passing the data to.
I have an ASP.NET 3.5 web form with a DropDownList bound to a table of company names in a database. I also have a data bound GridView which I would like to update with data from the database depending on the company name selected in the DropDownList, so that the SelectCommand for the GridView's SqlDataSource is:
SELECT Registration, Telephone, Profile FROM {CompanyName}_VehicleData
Where {CompanyName} is whatever is selected in the DropDownList. I've used the Command and Parameter Editor to create a ControlParameter pointing to the SelectedValue of the DropDownList, but I don't know how to write the SelectCommand query to concatenate the parameter to '_VehicleData'. Any thoughts.
If you are using a sqldatasource you could set the select command in the code behind.
<sqldatasource>.SelectCommand = "select registration, telephone, profile " & _
"from " & <dropdown>.selectedvalue & "_VehicleData"
<sqldatasource>.SelectType = SqlDataSourceCommandType.Text
#Colin: You said that the solution is writing a stored procedure that evaluates whether the concatenated value (DropDown.SelectedValue + "_VehicleData") maps to a real table using the INFORMATION_SCHEMA.TABLES view before injecting it into a SQL command. Can you please give a code snippet for the stored proc?
I am debugging code written by a previous developer :-)
I have a SQLDataSource control that is calling a database stored procedure, and passing quite a large number of paramaters, and the stored proc returns records that populates a gridview control.
When I walk through the code behind to determine the values being passed, is there any way that I can see exactly how the parameters are being formated and passed to the stored proc?
You can use SqlDataSource's Updating event for this. It's parameter SqlDataSourceCommandEventArgs has a property Command that contains the parameters for the query.
Instead of trying to unpick them from Code-behind, use Sql Profiler. If you are using the Express version, there's a Profiler for that too: AnjLab Sql Profiler
I have a table which links to another table in the ASP.NET membership schema.
Problem is, all the PKs for the ASP.NET tables are uniqueidentifier so mine has to be too. When I add a SqlDatasource and call its Insert() method, I get the following error:
Cannot insert the value NULL into column 'DiscountCode', table 'CreamDb.dbo.CustomInfo1'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The uniqueidentifier is also treated as an object (its data type), but there is no Guid data type. I had this problem before, but the schema was much simpler so I could fix it.
How can I go about fixing this? If I get rid of the data type part in the markup (so just leave the field/parameter name but not the data type stuff), I get another error so that is not possible.
Thanks
What do you mean by "there is no Guid data type"? What's wrong with System.Guid? Can't you just use Guid.NewGuid(), set the field appropriately, and do the insert?
EDIT: Just to give a bit more meat: attach an event handler to the Inserting event, and populate the field then, via the DbCommand returned by SqlDataSourceCommandEventArgs.Command. Or change the SQL used by the INSERT command to ask the database to populate the GUID field for you.
A popullar approach when dealing with references to the ASP.NET Membership Provider's data is, instead of keeping a proper foreign key to the GUIDs, instead store something like the LoweredUserName in your table. Then, use the Membership Provider's API to interact with the object you need. In some cases, you need an ObjectDataSource abstraction layer to accomplish CRUD scenarios.
Set the default value of the column in SQL Sever to "newid()".
Asp.net won't send the value, and the field will get a new guid.