Monday, March 12, 2012

How to handle null value

I got trouble with System.Null.ReferenceException. Please guide me how to manipulate it.

Below is my error code line, when the result of my query returns null value.

string FATypeName = SelectCommand.ExecuteScalar().ToString().Trim();

Thanks,


string FATypeName = SelectCommand.ExecuteScalar()
if ( FATypeName!=null )
{
FATypeName=FATypeName.ToString().Trim();
}
else
{
FATypeName=String.Empty;
}

Labels: , , , , , , , , , , , , , ,

How to handle errors when using GridView and DetailsView without writing any code

It is great that no code is neccessory when using GridView and DetailsView controls together with a SqlDataSource control. Since there is no code, "try.. catch.. finally" can not be used (nowhere to place the code block). I am wondering how the errors can be trapped and displayed. We certainly do not want users to see a generic .net error page. Can anyone give me some information on this? thanks,

Tom

I guess no one has noticed this question. Let me rephrase it. I really need some help on this. How can you use "try...catch..finally" to handle errors when working with GridView, DetailsView and SqlDataSource controls without writing any code? Or how and where to add error handling code? Thanks,
This is somewhat of a general answer to your question and it may not be of any help. You could use try-catch blocks that will run when events fired by your aspx page like Page_Load, On_Selecting etc... That way these error catching mechanisms will be in place to handle any abnormal behavior your code might produce during runtime.

Thanks plazma, for the suggestion. In fact, I had thought about the way you suggested. It did not work out (or at least I could not figure out how it works). The problem is this. Noramally, the data retrieval and data binding happen inside try.. catch block.

try{
//contact database and bind data to data controls
}
catch{
//handle error
}

However, for the codeless programming, data retrieval and data binding are done automatically by the controls. If the try..catch block is placed in Page_Load or other locations, it could catch some errors but they would not be the data retrieval and databinding related. If let's say there is a permission issue in database, this try..catch block will not be able to catch it because the data retrieval and databinding do not happen inside the block.


I found out that error can be handled in the events (selected, inserted and updated) of the SqlDataSource control:
protected void MySqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e){ if (e.Exception != null) { // Mask theerror with a generic message (for security purposes.) lblError.Text = "An exception occurred performing the query."; // Consider theerror handled. e.ExceptionHandled = true; }}

Labels: , , , , , , , , , , , ,

How to Handle dynamic Button Click event

Dear sir,

In the above code, I am binding the data such as description of the product, and i have added one button. When I click on the button for the particular row, that row's details has to be added in a separate datatable.


<asp:DataGrid ID="dgGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn >
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Add To Cart" Text="Add To Cart" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Please let me know how to proceed.

Thanks in Advance,

Regards,

Arun.

How about something like this:

protected void dgGrid_ItemCommand(object source, DataGridCommandEventArgs e){Button Button1 = e.CommandSourceas Button;if (Button1 ==null) {return; }DataGridItem dgi = Button1.NamingContaineras DataGridItem;if (dgi ==null) {return; }Label Label1 = dgi.FindControl("Label1")as Label;if (Label1 ==null) {return; }string description = Label1.Text;// Do whatever you want here}

Labels: , , , , , , , , , , , ,

how to handle datagrid item command in C#?

Can anyone share a code snippet to handle the link button with Update command in C#? The link button is a template column inside a datagrid.

ASPX code:
<asp:TemplateColumn HeaderText="Update">
<ItemTemplate>
<asp:LinkButton Runat="server" Text="Update" CommandName="Update" ID="Linkbutton1" NAME="Linkbutton1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn
I tried the following C# code:

private void grdOrderItems_ItemCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "Update")
{
...
}
}

But the handler did not get called when the link button was clicked.

Thanks.Hello, i recommend you check this pageAdding Button Columns to a DataGrid Control.

Good Luck.

Labels: , , , , , , , , , , , , , , ,