Here I am trying to add each line to a array position after validation to avoid blanklines. After that I want to concatenate another value to array positions 1st and to last for adding to backend database remaining lines I want to add as a individual records.
In this scnario I am geeting error while doing validation. Here I enclosed my code and as well as my errror page.
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:
Line 63:
Line 64: //foreach (string T in Txt)
Line 65: if (Txt1.Length > 0) Line 66: {
Line 67: Itime = DateTime.Now;
I am very much thankfull for your Ans & for suggetion. But some how I am unable to stop my code even after I insertion of break point. I am using VS2005
Thanks for your suggestion. This solution is partially OK becaz while if I commented a line which was adding a line into variable Txt1 from array then not throwing an error. I checked with below code. Here my task is to read text from textbox with multiple
line then I need to add one more value to 1st & to last line then I need to save to backend database as one line one record in database should avoid empty lines
TextReader read = new System.IO.StringReader(BreakingNews.Text);
int lines = 5;
string[] Txt = new string[lines];
for (int r = 0; r < lines; r++)
{
String Txt1 = "";
Txt[r] = read.ReadLine(); //adding to array to "r"th position
Txt1 = Txt[r]; //retriving a single line into Txt1 from array even it has blankline for validation
ravichoudary...
0 Points
9 Posts
Can anybody help me what's wrong in my code relating to textbox control with multiline
May 01, 2012 05:33 AM|LINK
Here I am trying to add each line to a array position after validation to avoid blanklines. After that I want to concatenate another value to array positions 1st and to last for adding to backend database remaining lines I want to add as a individual records. In this scnario I am geeting error while doing validation. Here I enclosed my code and as well as my errror page.
Thanks in advance
using
System;
using
System.Data;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Text;
using
System.Collections;
using
System.IO;
public
partial class BreakingTest : System.Web.UI.Page
{
SqlConnection Con;
SqlDataAdapter Da;
DataSet Ds;
String SqlQuery, InsTime, MyTxt, Heading, RecCount;
DateTime Itime;
int sno;
TextBox H, T;
ArrayList RowsToDelete;
protected void Page_Load(object sender, EventArgs e)
{
Con =
new SqlConnection(ConfigurationManager.ConnectionStrings["NtvTickerConnectionString"].ToString());
Con.Open();
if (IsPostBack == false)
{
//GetData();
}
//BDel.Attributes.Add("onClick", "return Confirm('Are you sure you want to delete selected item(s) ?');");
}
//protected void GetData()
//{
// Da = new SqlDataAdapter("select * from Breaking order by InsTime desc", Con);
// Ds = new DataSet();
// Da.Fill(Ds, "Breaking");
// DataView dv = new DataView(Ds.Tables["Breaking"]);
// RecCount = Convert.ToString(dv.Count);
// BreakingGrid.DataSource = Ds;
// BreakingGrid.DataMember = "Breaking";
// BreakingGrid.DataBind();
// BRecCount.Text = "Total no of records";
// BRecCountNo.Text = RecCount;
//}
protected void Button1_Click(object sender, EventArgs e)
{
TextReader read = new System.IO.StringReader(BreakingNews.Text);
int lines = 5;
string[] Txt = new string[lines];
for (int r = 0; r < lines; r++)
{
String Txt1;
Txt1 = read.ReadLine();
DisplayTxt.Text = Txt1;
//foreach (string T in Txt)
if (Txt1.Length > 0)
{
Itime =
DateTime.Now;
InsTime = Itime.ToString(
"yyyy-MM-dd hh:mm:ss");
String SqlQuery = "Insert into SuperBreaking values(N'" + Txt + "',N'" + Heading + "','" + InsTime + "')";
SqlCommand cmd = new SqlCommand(SqlQuery, Con);
cmd.CommandType =
CommandType.Text;
cmd.ExecuteNonQuery();
}
}
}
}
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 63: Line 64: //foreach (string T in Txt) Line 65: if (Txt1.Length > 0) Line 66: { Line 67: Itime = DateTime.Now;Source File: e:\NtvTicker\BreakingTest.aspx.cs Line: 65
Stack Trace:
nijhawan.sau...
All-Star
16460 Points
3178 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 01, 2012 05:50 AM|LINK
Can you put a breakpoint here and check the value:
Txt1 = read.ReadLine();
nijhawan.sau...
All-Star
16460 Points
3178 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 01, 2012 05:50 AM|LINK
Also put a Null statement check surrounding it
Ruchira
All-Star
44372 Points
7194 Posts
MVP
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 01, 2012 11:57 AM|LINK
Hello,
Assign the Txt1 variable to an Empty string before you check it in the below line
So, moidfy that line as below
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.ravichoudary...
0 Points
9 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 02, 2012 12:53 AM|LINK
Hi Nijhawan,
I am very much thankfull for your Ans & for suggetion. But some how I am unable to stop my code even after I insertion of break point. I am using VS2005
I tried with null values also.
ravichoudary...
0 Points
9 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 02, 2012 01:28 AM|LINK
Hi Ruchira,
Thanks for your suggestion. This solution is partially OK becaz while if I commented a line which was adding a line into variable Txt1 from array then not throwing an error. I checked with below code. Here my task is to read text from textbox with multiple line then I need to add one more value to 1st & to last line then I need to save to backend database as one line one record in database should avoid empty lines
TextReader read = new System.IO.StringReader(BreakingNews.Text);
int lines = 5;
string[] Txt = new string[lines];
for (int r = 0; r < lines; r++)
{
String Txt1 = "";
Txt[r] = read.ReadLine(); //adding to array to "r"th position
Txt1 = Txt[r]; //retriving a single line into Txt1 from array even it has blankline for validation
//DisplayTxt.Text = Txt1;
//foreach (string T in Txt)
if (Txt1.Length > 0)
{
Itime = DateTime.Now;
InsTime = Itime.ToString("yyyy-MM-dd hh:mm:ss");
String SqlQuery = "Insert into SuperBreaking values(N'" + Txt + "',N'" + Heading + "','" + InsTime + "')";
ravichoudary...
0 Points
9 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 02, 2012 01:47 AM|LINK
The above error is causing only while If I met a empty line or blank line But If there is any text not throwing any error.
thilsiva
Member
732 Points
146 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 02, 2012 05:21 AM|LINK
Hi,
Instead of checking
if(Txt1.Length >0) try the below code,
if(!string.IsNullOrEmpty(Txt1))
Hope it works!
ravichoudary...
0 Points
9 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 02, 2012 05:42 AM|LINK
Hi Thilsiva,
Very very greatfull to you such a good solution I got.
Thank you once again.
and
Is there any way to find total no of lines in multiline text box?
thilsiva
Member
732 Points
146 Posts
Re: Can anybody help me what's wrong in my code relating to textbox control with multiline
May 02, 2012 06:03 AM|LINK
Hi,
Try the below code,
string[] lines = Textbox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); int lineCount = 0; lineCount = lines.Length;Hope it helps!