Error handling when inserting a null value, asp.net c# - c#

i have a webpage built in asp.net c#. it always a user to create a new record in a db table. there are there are two input fields, text and score. text cannot be a null value so if the user doesn't input text onsubmit, the page errors out. i want to throw in some simple error handling code in the code behind page. i've tried including an if/else on_inserted method but ran into some java script errors. any help would be apprieciated. thanks.
aspx page -----------------
<EditItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</InsertItemTemplate>

why not use RequiredFieldValidator validator? it works inside the grid

<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="TextBox1"
Display="Static"
ErrorMessage="*"
runat="server"/>
Also don't forget to use page is valid before saving to the database.
if (page.isValid){
//send to db
}

Related

Accessing C# session varible in ASP.Net

I am trying to access a session variable in aspx but I think there is an issue with my syntax somewhere.
Under page load:
Session["UserName"]= username.Substring(8).ToString(); // This is ok as far as I know.
Issue is with the asp bit:
<asp:TextBox ID="ATextBox" runat="server" Text="<% Session["UserName"] %>" />
<asp:TextBox ID="ATextBox" runat="server" Text="<%# Session["UserName"] %>" />
I have also tried the above (one at a time) with .ToString() at the end too. But I keep getting errors:
Either The server tag is not well formed or the server tag contains % %
One thing that might make a difference is the Textbox is inside a GridView, (it is inside a ContentTemplate)but not a boundfield.
Try this:
<asp:TextBox ID="ATextBox" runat="server" Text='<%# Session["UserName"] %>' />
Not sure if it would help..

ASP Grid inside of Bootstrap popover using Eval Tags

I have a ASP Gridview that has a column with a button that opens a popover. Inside the popover I want to populate an ASP Table or ASP Gridview to show hidden related data. I am able to open the popover and it displays hard coded data. But when I want to make the data result dynamic using the Eval() tag the syntax is not able to read the text="<%# Eval('Label1') %>" tags within the string within the object.
The purpose is to load the entire data set and the popover data on one page load. I am wanting to avoid reload if possible. It seems that it cannot handle the <% within the string, is there a way to handle for this?
Any advice is welcome. Thank you in advance.
Sample code:
<asp:Button ID="Button3"
runat="server"
class="btn btn-info btn-xs"
Text="+"
OnClientClick="return false;"
data-toggle="popover"
data-trigger="focus"
TabIndex="0"
data-placement="right"
title="Owner Change"
UseSubmitBehavior="true"
data-content='<asp:Gridview runat="server" ID="gdvTest">
<Columns>
<asp:TemplateField HeaderText="Label One">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label1') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label Two">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label2') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
/>
Update: I also attempted to use ASP Table, but my class is not recognizing the ID im trying to use to dynamically populate the Label.
...data-content="<asp:Table><asp:TableRow><asp:TableHeaderCell text='TestHeader'><asp:Label runat='server' ID='lblTest' style='color:Red'></asp:TableHeaderCell></asp:TableRow></asp:Table></asp:Gridview>"
Your syntax is a bit off indeed. Remember that everything inside <%# %> should be a valid C#, so 'Label2' is an invalid literal. To fix that, swap quotes over:
text='<%# Eval("Label1") %>'
Update.
Missed the fact that GridView is itself surrounded by single quotes of data-content attribute. Even though this markup looks quite weird, there is a way to make it work, using double quotes everywhere and escaping appropriately:
text="<%# Eval("Label1") %>"

ASP Validators not working?

I feel embarrassed to be posting this as I'm pretty sure I'm missing something really simple, and this certainly isn't the first time I've used asp validators. I have the following validator controls:
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtTitle" EnableClientScript="false" ErrorMessage="*" ValidationGroup="groupUpdateAL" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAbbr" EnableClientScript="false" ErrorMessage="*" ValidationGroup="groupUpdateAL" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlColour" EnableClientScript="false" ValidationGroup="groupUpdateAL" ErrorMessage="*" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="groupUpdateAL" CausesValidation="true" /><br />
For some reason, if leaving the controls empty I am given the following error:
Length cannot be less than zero.
Parameter name: length
It seems the validation controls aren't catching an invalid input.
What I've Tried:
I suspected it may have been client validation intercepting the postback, which is why you can see EnableClientScript is set to false now. Didn't work.
Try testing page validity in the onclick event of your btnSave :
if(Page.IsValid)
{
//Do your stuff
}
else
{
Response.Write("error");
}
Validation is on new screen
in c# file avalilabel on this image plzz click
on this image
enter image description here
Create database to get querystring
Create connection file in visual studio

Set the GUID for the value at textbox FormView aspx

I've created FormView let user enter the data to save into database. And i need to disable one of the textbox and automatic set the GUID to the textbox. how can i do that?
<InsertItemTemplate>
RouteGUID:
<asp:TextBox ID="RouteGUIDTextBox" runat="server"
Text='<%# Bind("RouteGUID") %>' Enabled="False" />
<br />
you should Bind the FormView control on Page_Load..or should call the Formview.DataBind() method on page_load...as i can't see your full code, so said this as an assumption.

Accessing codebehind field values through ASP.NET HTML code

simple question
in code behind(.cs) we have
string error="11000114S";
in ASP (.aspx) we have:
<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage=(our string error="11000114S")>
So how to do this, assign value from cs-> saved error list to html ?
It is as simple as adding this to your .cs .. if I understand you correctly
string error="11000114S";
textValidator.ErrorMessage = error ; // or what ever you want
this should do
protected string error="11000114S";
<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage='<%# error %>'>
You can assign error messages dynamically to your validators in your code behind. I.e.:
this.textValidator.ErrorMesssage = error;

Categories

Resources