I'm trying to add a title for dropdown menu.
The problem is that if the data is null, it automatically update as 0.
This doesn't happen when I create the data row.
The date type is int32.
The funny thing is that I can insert null but I can not update as null.
My solution was to add two listitems.
One is value="". This is for initial data. (create)
Another is value="0". This is for after update.
However this is not pretty solution.
How come integer stores 0 instead of null when I update the data?
It seems possible to store null when I create the row...
<asp:DropDownList ID="addl" runat="server" SelectedValue='<%# Bind("a") %>'>
<asp:ListItem Value="" Text="-Select-"></asp:ListItem>
<asp:ListItem Value="1">test1</asp:ListItem>
<asp:ListItem Value="2">test2</asp:ListItem>
<asp:ListItem Value="0" Text="-Select-"></asp:ListItem>
</asp:DropDownList>
what would be the best solution for dropdown menu title if the data type is integer?
If it's string, I can simply add value="" and works just time.
Integer stores 0 if it's null and I have to also create null in order to avoid error in edit mode.
Int32 is not a Nullable type and by default will equal 0 (even if not initialized). Try using Int32? which is Nullable, or use some other index to indicate the Create option.
I'm not quite sure what int you are trying to update, but if it's a variable that you have control over you could look at changing it from a standard int to a nullable int. This can be as simple as changing this:
int i;
to this:
int? i;
Related
I have 2 textboxes that use type=date parameter that shows date like dd/MM/yyyy
<asp:TextBox Type="date" runat="server" ID="txtScadenza" name="txtScadenza" placeholder="Scadenza" required="required" AutoPostBack="true" class="form-control input-md"></asp:TextBox>
<asp:TextBox Type="date" runat="server" ID="txtScadenzaDip" name="txtScadenzaDip" placeholder="Scadenza" AutoPostBack="false" class="form-control input-md" ></asp:TextBox>
Those textboxes receive a date with this code:
txtScadenza.Text = Cliente.Scadenza.ToString("yyyy-MM-dd")
txtScadenzaDip.Text = Utente.dataScadenza.ToString("yyyy-MM-dd")
Both fields, Scadenza and DataScadenza are DateTime fields on a SQL db. The only difference is that DataScadenza is nullable, Scadenza is not.
I'm receiving the error in the title ONLY on txtScadenzaDip, instead txtScadenza is showing the date with the right format... it's driving me insane. When receiving the error I know that the field DataScadenza has this value: 2021-09-17 00:00:00.000
Where is the problem, please?
I find out that there is much difference between a simple DateTime object and a Nullable DateTime object, here what I needed to do to make it work:
Dim dataScadenza As DateTime
If Utente.dataScadenza.HasValue Then
dataScadenza = Utente.dataScadenza
txtScadenzaDip.Text = dataScadenza.ToString("yyyy-MM-dd")
End If
EDIT
As jmcilhinney has pointed out, there's another and simpler solution, using the ? operator:
txtScadenzaDip.Text = Utente.dataScadenza?.ToString("yyyy-MM-dd")
SQL Server: I have 3 columns [ID], [Text], [Value]
ASPX page:
I have a dopdownlist populated by a SqlDataSource:
<asp:DropDownList ID="ddl" runat="Server" DataSourceID="sql" DataValueField="ID" DataTextField="Text" />
<asp:SqlDataSource ID="sql" runat="Server" SelectCommand="sp" SelectCommandType="StoredProcedure" />
Using DataValueField and DataTextField I can capture 2 of the values in the SQL Server table based on selected value.
Question:
How to capture the additional column [Value] based on the users #ddl selected value?
Notes:
I can achieve this using javascript to populate a matching dropdown list using the [Value] field instead of the [ID] field and match on [Text] field.
There must be a more efficient and appropriate way to handle this with just asp or VB.
ANSWER USED
I simply got the value at time of use using the ddl [ID] and a sql select statement.
SELECT #Value = [Value] FROM [tbl] WHERE [ID] = #ID
Passing in #ID as the dropdownlist selectedvalue.
I'm guessing the ID is a unique identifier of the row, so why would you need the text or value columns to be submitted by the form? You can just retrieve these from the database using the ID that is submitted. Or am I missing something here?
Hi all I am having an integer value null in my table, I would like to bind it to gridview label with 0 when the value is null, for a nullable string I write this which works fine but the same with changes didn't work can some one help
<asp:Label ID="lbl" runat="server" Text='<%#(String.IsNullOrEmpty(Eval("call").ToString()) ? "NULL" : Eval("call"))%>'></asp:Label>
The same for Integer I write as follows
<%# string.IsNullOrEmpty(Eval("send2").ToString()) ? "0" : Convert.ToInt16(Eval("send2")).ToString() %>
This didn't worked, any help appreciated
To check for the null variables you usually use the System.DBNull , so you code can be:
Eval("send2")==System.DBNull ? "0" : Convert.ToInt16(Eval("send2")).ToString()
or aleternative:
Convert.IsDBNull(Eval("send2")) ? "0" : Convert.ToInt16(Eval("send2")).ToString()
How can i have a wildcard in a where clause to where it will show ALL in a certain situation?
I have a dropdown list that contains:
All - (no value)
Staff (value = Staff)
Manager (value = Manager)
Distributor (value = Distributor)
My SQL select statement originally was:
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType)
The Role Type equals either Staff, Manager or Distributor and is never blank. This works great to filter by each of the 3 roles, but how can i make it so when ALL is selected, it shows all instead?
I believe i need to do something like this, but can't get it right:
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType OR #RoleType <> NULL)
ANSWER:
I couldn't leave the DDL value for ALL empty, so this is what it looks like and works now.
<asp:ListItem Text="All" Value="All" Selected="True"></asp:ListItem>
<asp:ListItem Text="Staff" Value="Staff"></asp:ListItem>
<asp:ListItem Text="Manager" Value="Manager"></asp:ListItem>
<asp:ListItem Text="Distributor" Value="Distributor"></asp:ListItem>
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType OR #RoleType = 'ALL')
SELECT * FROM [DB_Table] WHERE ([Site] = #Site) AND (RoleType=#RoleType OR #RoleType is NULL)
I think it should be null RoleType for the All drop down choice right?
I'm using an EntityDataSource and creating a projection using the CommandText property to query across several tables. I want to allow paging, but when I run the code I get an error that says
For the EntityDataSource, if the query specifies a projection and
paging is enabled, a sort expression must be defined. Either set the
OrderBy property or set AutoGenerateOrderByClause to true
Odd thing is, I HAVE set the AutoGenerateOrderByClause to true and the error persists. I'm not sure what to do at this point. Here's my example EntityDataSource code.
<asp:EntityDataSource runat="server" ID="EntityDataSource"
ConnectionString="name=AssetRegistryEntities"
DefaultContainerName="AssetRegistryEntities"
CommandText="SELECT a.astName, a.astDescription, r.rolFK_adCN
FROM AssetRegistryEntities.Assets as a
JOIN AssetRegistryEntities.Roles as r ON r.rolFK_astID = a.astID
WHERE r.rolFK_adCN = 'dpellerin'
AND r.rolTypeCode = 'PRIANALYST'"
AutoGenerateOrderByClause="true">
</asp:EntityDataSource>
Anyone know how to make paging work with this?
You have a projection. Either get rid of the SELECT or also add OrderBy="it.astID"
Just to clarify the accepted answer of AFD.
Change the EntityDataSource to the following:
<asp:EntityDataSource runat="server" ID="EntityDataSource"
ConnectionString="name=AssetRegistryEntities"
DefaultContainerName="AssetRegistryEntities"
OrderBy="it.astID"
CommandText="SELECT a.astName, a.astDescription, r.rolFK_adCN
FROM AssetRegistryEntities.Assets as a
JOIN AssetRegistryEntities.Roles as r ON r.rolFK_astID = a.astID
WHERE r.rolFK_adCN = 'dpellerin'
AND r.rolTypeCode = 'PRIANALYST'"
AutoGenerateOrderByClause="true">
</asp:EntityDataSource>