Link to home
Start Free TrialLog in
Avatar of Ianaldo
Ianaldo

asked on

VB to C# error

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

Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

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).
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".
Avatar of openshac
openshac

Are all your objects declared (on the page and in the code behind)?

I can't see where DetailsView1.ItemCommand  is declared
Avatar of Ianaldo

ASKER

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

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...
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...
Avatar of Ianaldo

ASKER

So what changes do i need to make to the code?
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.  
ASKER CERTIFIED SOLUTION
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Ianaldo

ASKER

Yes thats it, thanks alot! :)