Galina Besselyanova
asked on
c# System.NullReferenceException: Object reference not set to an instance of an object
Hello,
Please, help me to find out what is wrong. The code is below.
This is asp code:
Everytime i click on Create button i get error:
"System.NullReferenceExcep tion: Object reference not set to an instance of an object. at AddNewResponses() "
Any ideas?
Thank you.
Please, help me to find out what is wrong. The code is below.
This is asp code:
<asp:Literal ID="LtError" runat="server" />
<asp:PlaceHolder ID="PhWrapper" runat="server">
<h2>Create New Ballot</h2>
<asp:MultiView ID="MvNewBallot" runat="server" ActiveViewIndex="0">
<asp:View ID="ViewForm" runat="server">
<div class="generic-form-section" style="text-align:center";>
<strong>Candidats:</strong><br />
<p><strong>1.</strong><asp:TextBox ID="Cand1" Width="313px" runat="server" /><br />
Information Link:<asp:TextBox ID="TxtLink1" Width="313px" runat="server" /><br /></p>
<p>2.</strong><asp:TextBox ID="Cand2" Width="313px" runat="server" /><br />
Information Link:<asp:TextBox ID="TxtLink2" Width="313px" runat="server" /><br /></p>
<p><strong>Status: </strong><asp:DropDownList ID="DdlStatus" runat="server">
<asp:ListItem Text="Active" Value="Active" />
<asp:ListItem Text="Inactive" Value="Inactive" Selected="True" />
</asp:DropDownList></p>
</div>
<p>
<asp:LinkButton ID="BtnCreate" runat="server" Text="Create Ballot" CssClass="btn" OnClick="BtnCreate_Click" />
</p>
</asp:View>
<asp:View ID="ViewThanks" runat="server">
<p>The Ballot has been successfully created!</p>
</asp:View>
</asp:MultiView>
</asp:PlaceHolder>
And in C#:protected void BtnCreate_Click(object sender, EventArgs e)
{
try
{
AddNewResponses();
MvNewBallot.SetActiveView(ViewThanks);
}
catch (Exception ex)
{
Response.Write(ex);
LtError.Text = "An error has occurred while attempting to submit your application. <span style=\"display: none;\">" + ex.ToString() + "</span>";
}
}
private void AddNewResponses()
{
CDataAccess da = null;
using (da = new CDataAccess(_connectionString))
{
da.OpenConnection();
int resseq = 0;
foreach (Control ct in this.Controls)
{
TextBox tb = ct as TextBox;
if (tb.ID.Contains("Cand"))
{
string rlink = "";
if (!string.IsNullOrWhiteSpace(tb.Text)) // (tb.Text.Length > 0) //&
{
++resseq;
da.SelectParameterizedStoredProcedure("AddNewResponse");
da.AddInputObject("ResSeq", System.Data.SqlDbType.Int, resseq);
da.AddInputObject("ResText", System.Data.SqlDbType.VarChar, tb.Text);
if (tb.ID == "Cand1")
{
rlink = TxtLink1.Text;
}
if (tb.ID == "Cand2")
{
rlink = TxtLink2.Text;
}
if (!String.IsNullOrWhiteSpace(rlink))
{
da.AddInputObject("ResLink", System.Data.SqlDbType.VarChar, rlink);
}
da.ExecuteParameterizedCommand();
da.ClearResultsAndParameters();
da.CloseConnection();
da.Dispose();
}
}
}
}
}
Everytime i click on Create button i get error:
"System.NullReferenceExcep
Any ideas?
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works like a charm!
Thank you for pointing me in right direction! You saved me a lot of time.
Thank you for pointing me in right direction! You saved me a lot of time.
Open in new window
...can result in null if the thing you are attempting to cast is not actually a TextBox. Are you certain that all of the controls you are looping over are actually TextBoxes?