How to convert asp.net NULL value to "" (blank string) - asp.net

I have a situation where I am using a detailsview with the SQLDataSource to update a record in an SQL Server Database.
My issue is that textfields that are not completed are converted to NULL in the database. This causes issue as the website using third party web services that can not handle to NULL DB value.
Is there a way that if the textfield is left empty that when the update or insert occurs, that the data is converted to "" (blank string) instead of the NULL value?

Since you are using SQLDataSource, when you insert or update a record, there is a ConvertEmptyStringToNull property of the Insert/update parameters. Set this to false, and set the string to empty, if no value is provided to that control. Look at the example below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server">
<InsertParameters>
<asp:ControlParameter ConvertEmptyStringToNull="false" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter ConvertEmptyStringToNull="false" />
</UpdateParameters>
</asp:SqlDataSource>

You could set a default value of "" on the database table column. Or in your SQL select statements you could do something like
SELECT IsNull(ColName,"")
FROM SomeTable
The

Related

Error: Cannot insert the value NULL into column

I have a gridview tied to a sqldatasource. Trying to run the update stored proc.
I keep getting this error...
Cannot insert the value NULL into column 'MyFKId', table 'dbo.MyTable';
column does not allow nulls. INSERT fails. The statement has been
terminated.
I know the answer seems obvious, that I am trying to insert a null into a column that doesn't allow it. The problem is that I know I am putting a value into 'MyFKId'. In my code I set a breakpoint and saw there was a value after this line ran...
dsMyDataSource.UpdateParameters.Item("MyFKId").DefaultValue = SomeId
SomeId did indeed have a value. It was not null. What do you think could be wrong? The SQLDataSource is setup as follows...
<asp:SqlDataSource ID="dsMyDataSource" runat="server" ConnectionString="xxxxx"
SelectCommand="xxxxx" SelectCommandType="StoredProcedure"
UpdateCommandType="Text"
UpdateCommand=" Execute s_MyStoredProc #MyFKId,...#UserId">
<UpdateParameters>
<asp:Parameter Name="MyFKId" Type="Int32" />
...
</UpdateParameters>
</asp:SqlDataSource>
Don't use EXECUTE to call your stored procedure. Use InsertCommand
<asp:SqlDataSource ID="dsMyDataSource" runat="server" ConnectionString="xxxxx"
InsertCommand="s_MyStoredProc" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:Parameter Name="MyFKId" Type="Int32" />
...
</InsertParameters>
</asp:SqlDataSource>
And use InsertParameters to set the parameter.
dsMyDataSource.InsertParameters("MyFKId").DefaultValue = SomeId
Can not try it but hope that will solve it for you.

ASP.NET SqlDataSource, like on SelectCommand

I'm working on asp.net. I Have a SqlDataSource with a query hardcoded on selectcommand:
<asp:SqlDataSource ID="DataSource1" runat="server" CancelSelectOnNullParameter="False"
ConnectionString="<%$ ConnectionStrings:S.Properties.Settings.ConnectionString %>"
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like Case #col1_param When null Then col1 Else #col1_param End)
and ([col2] like Case #col2_param When null Then col2 Else #col2_param End)"
SelectCommandType="Text">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="col1_param" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="TextBox2" Name="col2_param" PropertyName="Text"
Type="String" />
</SelectParameters>
What I want is that if you enter data on one textbox only, the data will display according with that textbox value only on the where clause. And if no values are placed for neither of the textboxes, the the query executes as if there is no where.
Right now with this code,what happens is if you put on one textbox only no data is displayed. The same if all textboxes are empty.
I don't want to use sql stored procedure.
How can I solve this?
Thanks...
Assuming it passes null when there is no text entered, otherwise you will need to check for the empty string
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like '%#col1_param%' or #col1_param is null)
and ([col2] like '%#col2_param%' or #col2_param is null)"
It sounds like you want your query to optionally search a column.
You can use the format
WHERE #col1_param IS NULL OR [col1] LIKE '%#col1_param%'
to property handle the case where the parameter is not specified.
See my question on the issue for a full answer. Granted it was done as a stored procedure, but the concept will hold the same for your SQLDataSource.

INSERT INTO error message

I created a web application with Visual Web Developer in ASP.NET and a SQL Server database. In SqlDataSource I made connection with INSERT INTO.
So my code is:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Table1]"
InsertCommand="INSERT INTO [Table1] ([Name(nick)], [E-mail], [Text]) VALUES (#Name(nick), #E-mail, #Text)">
<InsertParameters>
<asp:Parameter Name="Name(nick)" />
<asp:Parameter Name="E-mail" />
<asp:Parameter Name="Text" />
</InsertParameters>
</asp:SqlDataSource>
Everything looks well without any errors. When I view my project in web browser or I start debugging I have normal web page. Then I type some text, click to send button and error in application.
Description: Incorrect syntax near '('. Must declare the scalar
variable "#Name".
Specifications: System.Data.SqlClient.SqlException: Incorrect syntax
near '('. Must declare the scalar variable "#Name".
In my table I have all this values and in my web page I use it as well. Could you help me what is wrong?
Many thanks!
Replace with this query
INSERT INTO Table1 (Name, E-mail, Text) VALUES (#Name, #E-mail, #Text)
(nick) create problem in your query
InsertCommand="INSERT INTO Table1 (Name, E-mail, Text) VALUES (#Name, #E-mail, #Text)">
The syntax seems incorrect.
You need to list the field names then the parameters names
I suppose the correct field is Name and you have a parameter called #nick but, as far as I know could be also the reverse.
InsertCommand="INSERT INTO [Table1] ([Name], [E-mail], [Text]) VALUES (#nick, #E-mail, #Text)"
As a side note, try to avoid to use the name of a data type (TEXT) as field names
Parameter containing brackets Name(nick) is not allowed, and it is causing the problem.
Just replace it in the InsertParameters and in the InsertCommand.
InsertCommand="INSERT INTO [Table1] ([Name], [E-mail], [Text]) VALUES (#Name, #E-mail, #Text)">
-- [Name] is considered as column in the table
<asp:Parameter Name="Name" />

Use a DDL to select records in a gridview VB.NET

I've got a simple DDL with values like 1, 2, 3.
I have a Gridview with a BoundField DataField="Cycle"
I want to pick a value from the Drop Down List and I want to update my Gridview to only show the records where Cycle = the picked value.
I'm doing this instead of using a Textbox with a Submit button.
When I have my connection string setup, I can test the query and it works, I just can't get the selected/updated value of the drop down List to change the gridview.
Thanks, Bill
You need to handle the DDL aka ComboBox SelectedIndexChanged event. In that event query your DB fetching the data based on the SelectedItem value.
I found out that I created the gridview before the DDL and that threw me off as the datasource was already setup. I ended up redefining the gridview connection and had as it's control parameter the DDL. With that done when I select a new value the GridView refreshes the query.
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/FromHost06192012_11/FromHost.mdb"
SelectCommand="SELECT * FROM [Table] WHERE ([Cycle] = ?) ORDER BY [Route]">
<SelectParameters>
<asp:ControlParameter ControlID="**DropDownList1**" DefaultValue="%" Name="Cycle2" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:AccessDataSource>

How to set parameters for SqlDataSource UpdateCommand

For a Gridview:
I am trying to use a stored procedure for the first time in a SQLDataSource for the UpdateCommand:
<asp:SqlDataSource ID="TECT_DataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>"
ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName %>"
SelectCommand="SELECT MPID, User_Id, Last_Name, First_Name
FROM Scripts.vw_Tect_Exam"
UpdateCommand="P_TECT_UPD_EXAM_ID" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="MPID" Type="Int32" />
<asp:Parameter Name="User_Id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
I am wondering how the UpdateParameters get their values set, since I only specify a name?
The procedure P_TECT_UPD_EXAM_ID expects two parameters as input: "in_MPID" and "in_UserId"
I am also wondering how to map those values to the input parameters for the procedure as the names are different?
You can set them something like this :
Example : In the example MPID is the sql parameter name #MPID
<UpdateParameters>
<asp:ControlParameter Name="MPID" ControlID="MPID_TextBox" PropertyName="Text />
<asp:ControlParameter Name="User_Id" ControlID="User_Id_TextBox" PropertyName="Text />
</UpdateParameters>
Correction: Just spotted your proc param names so it must be
<asp:ControlParameter Name="in_MPID" ...............
<asp:ControlParameter Name="in_User_Id" ...............
Hope this helps....
I really wouldn't use a SqlDataSource. It will be much easier if you make the call to the database in the code-behind (or a better yet in a Data Access Layer).
If you use a SqlDataSource the stored procedure call will only be available on that page. Every time you want to make that same call you will have to copy and paste the SqlDataSource or make a UserControl out of it.
The following example uses the Entity Framework to connect to the database and retrieve records:
public List<Record> GetAllRecordsByUserName(string credentials)
{
List<Record> recordList;
using (CustomEntities context = new CustomEntities())
{
IQueryable<Section> recordQuery = from records in context.Records
where records.UserName == credentials
select records;
recordList = recordQuery.ToList<Record>();
}
return recordList;
}

Resources