Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB to C# error

Posted on 2009-04-29
11
Medium Priority
?
258 Views
Last Modified: 2012-05-06
HI Guys,

Ive used c-sharpener to convert vb code to c#, which has done for the most part, but it has left a few errors, i get this message "Object reference not set to an instance of an object."

Line 11:         // events handled by DetailsView1_ItemCommand
Line 12:         /* TRANSWARNING: check EventHandler assignment */
Line 13:         DetailsView1.ItemCommand += new System.Web.UI.WebControls.DetailsViewCommandEventHandler( DetailsView1_ItemCommand );
Line 14:     }

Any ideas why this is doing this, and if you could fix up my code, and show me how i should go about these kind of changes?

Thanks guys, code is attached :)
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient; 
using System.Net.Mail; 
 
// TRANSMISSINGCOMMENT: Class Admin_CreateSurvey
partial class Admin_CreateSurvey : System.Web.UI.Page 
{ 
    public Admin_CreateSurvey() 
    { 
        // events handled by DetailsView1_ItemCommand
        /* TRANSWARNING: check EventHandler assignment */ 
        DetailsView1.ItemCommand += new DetailsViewCommandEventHandler( DetailsView1_ItemCommand ); 
    } 
    
    // TRANSMISSINGCOMMENT: Method DetailsView1_ItemCommand
    protected void DetailsView1_ItemCommand( object sender, DetailsViewCommandEventArgs e ) 
    { 
        if ( e.CommandName == "Invite" ) 
        { 
            SqlConnection cnn = new SqlConnection( ConfigurationManager.ConnectionStrings[ "ConnectionString" ].ConnectionString ); 
            SqlCommand cmd = new SqlCommand( "select * from surveyparticipants where surveyid=@id", cnn ); 
            SqlParameter p1 = new SqlParameter( "@id", ( ( System.Data.SqlDbType )( DetailsView1.SelectedValue ) ) ); 
            cmd.Parameters.Add( p1 ); 
            SqlDataAdapter da = new SqlDataAdapter(); 
            da.SelectCommand = cmd; 
            DataSet ds = new DataSet(); 
            da.Fill( ds, "participants" ); 
            SmtpClient client = new SmtpClient(); 
            foreach ( System.Data.DataRow row in ds.Tables[ "participants" ].Rows ) 
            { 
                client.Send( ConfigurationManager.AppSettings[ "webmasteremail" ], System.Convert.ToString( row[ "email" ] ), "Invitation to participate in our survey", "Please take the survey at the following URL :" + "\r\n" + "http://" + Request.Url.Host + "/survey.aspx?id=" + DetailsView1.SelectedValue ); 
            }
            Label3.Text = "Invitations sent successfully!"; 
        } 
    } 
    
}

Open in new window

0
Comment
Question by:Ianaldo
  • 7
  • 3
11 Comments
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24259923
This error: "Object reference not set to an instance of an object."
mean that you are trying to access a property of an object who has not been assigned, or of you prefer: a null object.

DetailsView1.ItemCommand += new System.Web.UI.WebControls.DetailsViewCommandEventHandler( DetailsView1_ItemCommand );

In this case, make sure that DetailsView1 is assigned (Trace your code, i can't do it for you since i don't have the source).
0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24259943
I don't know c-sharpener, but i guess that it translated a form from VB, and he had to create a DetailView object.  So make sure you find it and that it's named "DetailView1".
0
 
LVL 6

Expert Comment

by:openshac
ID: 24259961
Are all your objects declared (on the page and in the code behind)?

I can't see where DetailsView1.ItemCommand  is declared
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:Ianaldo
ID: 24259975
Well if this is of any help, attached is the source code for the main page for that codebehind i sent prior:

Thanks
<%@ Page Language="C#" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="ManageSurvey.aspx.cs" Inherits="Admin_CreateSurvey" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label2" runat="server" Text="<h3>Manage Suryeys</h3>"></asp:Label><asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
        CellPadding="4" DataKeyNames="SurveyID" DataSourceID="SqlDataSource1" ForeColor="#333333"
        GridLines="None" Height="50px" Width="100%">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <EditRowStyle BackColor="#999999" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <Fields>
            <asp:BoundField DataField="SurveyID" HeaderText="SurveyID" InsertVisible="False"
                ReadOnly="True" SortExpression="SurveyID" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:TemplateField HeaderText="Description" SortExpression="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Description") %>' TextMode="MultiLine"></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Description") %>' TextMode="MultiLine"></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert"
                        Text="Insert"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel"></asp:LinkButton>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="Edit"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="New"
                        Text="New"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
                        Text="Delete"></asp:LinkButton>
                    <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Invite">Send Invitations</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" Width="20%" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:DetailsView>
    <asp:Label ID="Label3" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="Red"></asp:Label><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        DeleteCommand="DELETE FROM [Survey] WHERE [SurveyID] = @SurveyID" InsertCommand="INSERT INTO [Survey] ([Title], [Description]) VALUES (@Title, @Description)"
        SelectCommand="SELECT * FROM [Survey]" UpdateCommand="UPDATE [Survey] SET [Title] = @Title, [Description] = @Description WHERE [SurveyID] = @SurveyID">
        <DeleteParameters>
            <asp:Parameter Name="SurveyID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="SurveyID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>

Open in new window

0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24260413
I don't know why... but your DetailsView1 seems to be "null" for some reason.
You can trace your code...

Set a breakpoint in your constructor, and check for DetailsView1...

Let me some time to figure out...
0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24260784
I think its normal.  

I don't think we should interpret the constructor as the equivalent of "Page_Load" in VB.NET

DetailsView1 doesn't exist yet when the constructor is called...
0
 
LVL 2

Author Comment

by:Ianaldo
ID: 24260927
So what changes do i need to make to the code?
0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24261089
That's what i need to find.   As i said, i'm not a C# programmer.  I've declared a button on your page, and move this line into its click event:

DetailsView1.ItemCommand += new System.Web.UI.WebControls.DetailsViewCommandEventHandler( DetailsView1_ItemCommand );

and everything work perfectly.  But now i've to figure out how to do this call just after the page load, not before the page load.  
0
 
LVL 10

Accepted Solution

by:
Christian de Bellefeuille earned 2000 total points
ID: 24261121
There...

Remove the line from the constructor, and add this function:


    public void page_Load()
    {
        this.DetailsView1.ItemCommand += new DetailsViewCommandEventHandler(DetailsView1_ItemCommand);
    }

Open in new window

0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24261138
I don't understand why, but in Visual Studio 2005, i can't see any "undefined methods" in the methods pull-down...  this is why it took me so long.
0
 
LVL 2

Author Comment

by:Ianaldo
ID: 24261309
Yes thats it, thanks alot! :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question