Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



Closed Thread
C# Reading From Database - Need to find and use special characters.
Old 04-19-2010, 01:50 PM C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
OK, here's the situation. If you've followed some of my older posts begging for information, you'll remember that I'm usually a vb.net guy. BUT, things being what they are - I'm getting started in a grown-up language (or so my co-workers tell me).

Project History
So, we have this old "Ticket system" that we were doing by contract with another company... last year we upgraded with them, and it changed the way the data was stored... instead of everything going into 3 fields, Ticket Number Date Added and Comments... It now gets listed in 4 fields... the new field "Added By" has been... well, added to the table.

Sine we decided that it was high time to bring the work in-house, we are recreating the interface and how the data is stored.

So, some fields have data and others do not.

The field "AddedBy" was originally in the comments - but no longer the case.

The Question
How do I look for special characters in the table's field to separate the fields on the user interface side? (the special character is nothing more than the HTML closing bold tag... well, "</b>") I need to be able to search for and identify the closing bold tag then use that to break up the text in the field.

Thoughts?
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
 
Register now for full access!
Old 04-19-2010, 02:32 PM Re: C# Reading From Database - Need to find and use special characters.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Is it the ONLY closing "</b>" ?


And is that the end of the data as well?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
View Public Profile Visit chrishirst's homepage!
 
Old 04-19-2010, 03:19 PM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Yup, that's how I know where I'm breaking the data. There's some data after the "</b>"...
essentially, it used to read like this:
[date-time-stamp] - [added by and ownership group] - [comments]

now reads like this
[date-time-stamp] - [added by] - [ownership group] - [comments]

(this is where I need to make it all uniform... for the older style, I need to look for the </b> where-as, in the newer stuff - I don't.)

So for consistency with the data and output, I would like it to be similar...
In the first row of my table will be date, added by, ownership, and the comments will be in the second row.
(in the back end, I have a loop that goes on and creates rows as necessary.)

Code:
<table>
<tr>
<td>
<b>[date-time-stamp] - [added by] - [ownership group]</b> <=this is where I need to break it
</td>
</tr>
<td>
[comments]
</td>
</tr>
</table>
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-19-2010, 03:27 PM Re: C# Reading From Database - Need to find and use special characters.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok what database are you using because it may be simpler to write a query to extract the data from one column and move it to another.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
View Public Profile Visit chrishirst's homepage!
 
Old 04-19-2010, 03:36 PM Re: C# Reading From Database - Need to find and use special characters.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
And of course how is the data arranged in the table.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
View Public Profile Visit chrishirst's homepage!
 
Old 04-20-2010, 10:24 AM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
The Database is MSSQLServer (05)

The columns are as described earlier...
Quote:
it used to read like this:
[date-time-stamp] - [added by and ownership group] - [comments]

now reads like this
[date-time-stamp] - [added by] - [ownership group] - [comments]
I would love to just extract those bits of data out of the old field, all together, but have no direct access to change anything in the database... so, i need to do it programatically on the user interface. *sigh*
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-20-2010, 02:01 PM Re: C# Reading From Database - Need to find and use special characters.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yep BUT What defines the END of the data AFTER the </b>

</b>some information [delimiter]

It would be a couple of "string slicing" UPDATE query so you need to know where the start and end of the string slice will be.

Assuming of coure you're allowed to write and run queries, and knowing some DBA's. If the query hasn't been submitted, tested, edited to make it less efficient because "It's better like that" and then resubmitted and re-tested (if you are lucky) you won't be allowed to!
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
View Public Profile Visit chrishirst's homepage!
 
Old 04-21-2010, 11:29 AM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Oh, I see...

after the "</b>" there's text... and that's it. there's no other delimiter, that's the end of the field.

HOWEVER
Here's a thought.... I was presented with an idea...

Load a repeater with the data... instead of separating the fields, we load it according to whether or not the "AddedBy" field is null or not.

That's all fine and dandy, but my issue is... what is the syntax to find out if the filed "AddedBy" is empty and populate the repeater accordingly?

I started with this (and I know where my issue lies, but how do I fix it?):
Code:
protected void loadRepeater(String TicketNumber)//this is being carried via session variable
        {
        //load the repeater with the comments of the ticket(s)

        //create the SQL Connection
        SqlConnection sqlConn_Comments = new SqlConnection(ConfigurationManager.ConnectionStrings["My_ConnectionString"].ConnectionString);

        //Create the SQL Command
        SqlCommand sqlCmdComments = new SqlCommand("Get_TicketComments", sqlConn_Comments);
        sqlCmdComments.CommandType = CommandType.StoredProcedure;
        
        //Add the parameters needed for the query
        sqlCmdComments.Parameters.Add(new SqlParameter("@TixNumber", TicketNumber));

        //Open the connection to the DB
        sqlConn_Comments.Open();

        //Read the data
        SqlDataReader dr_Comments = sqlCmdComments.ExecuteReader();

        if (dr_Comments.Read())
            {
            //Create the repeater
            Repeater Repeater1 = new Repeater();

            //Create the repeater's data source
            Repeater1.DataSource = dr_Comments;

            //bind the data to the repeater
            Repeater1.DataBind();

            foreach (RepeaterItem repeateItem in Repeater1.Items)
                {
                if (repeateItem.ItemIndex == 0)
                    {
                    RepeaterItem headerItem = new RepeaterItem(repeateItem.ItemIndex, ListItemType.Header);
                    HtmlGenericControl hTag = new HtmlGenericControl("h4");
                    hTag.InnerHtml = "&nbsp;";
                    repeateItem.Controls.Add(hTag);
                    }

                //If AddedBy is null, then populate accordingly
                if (String.IsNullOrEmpty("AddedBy")) //<==this is where my issue lies... what is the correct syntax to find out if the db field is empty?
                    {
                    Label lbl = new Label();
                    RepeaterItem container = (RepeaterItem)lbl.NamingContainer;
                    String strVals = Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "TicketStatus")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "Comments"));
                    lbl.Text = strVals;
                    }
                //otherwise load the repeater this way.
                else
                    {
                    Label lbl = new Label();
                    RepeaterItem container = (RepeaterItem)lbl.NamingContainer;
                    String strVals = Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "TicketStatus")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "AddedBy")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "DateAdded")) + "<br />" + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "Comments"));
                    lbl.Text = strVals;
                    }
                }
            //add the repeater to the panel
            Panel1.Controls.Add(Repeater1);
            }

        //close and dispose...
        dr_Comments.Close();
        dr_Comments.Dispose();
        sqlConn_Comments.Close();
        sqlConn_Comments.Dispose();
        }

-sincerly, confused...
(I thank you in advance for your knowledge and for your patience)
__________________
Need a vacation.

Last edited by mb2000inc; 04-21-2010 at 11:32 AM..
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-22-2010, 10:00 AM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Come on, chrishirst. don't give up on me, please. I need the assistance.

Thanks.
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-22-2010, 11:11 AM Re: C# Reading From Database - Need to find and use special characters.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
if (String.IsNullOrEmpty(DataBinder.Eval(((RepeaterItem)container).DataItem, "AddedBy")) {
Looks pretty close (without trying to recreate the whole thing
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
View Public Profile Visit chrishirst's homepage!
 
Old 04-22-2010, 11:14 AM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Oh, cool Let me give that a shot! Thanks... brb...
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-22-2010, 11:27 AM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
nope... tried it and got this... before I got to test it...




I tried something like this and it still doesn't work... I'm kind of at a loss....
__________________
Need a vacation.

Last edited by mb2000inc; 04-22-2010 at 11:29 AM..
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-23-2010, 02:46 PM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Come on, buddy... we're getting close - I can feel it! Don't give up.

I tried this...
Code:
if (dr_Comments.HasRows && dr_Comments["AddedBy"] != DBNull.Value)
And now have a new error...
"Object reference not set to an instance of an object."

Set on this line:
Code:
String strVals = Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "TicketStatus")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "AddedBy")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "DateAdded")) + "<br />" + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "Comments"));
Thoughts?
__________________
Need a vacation.

Last edited by mb2000inc; 04-23-2010 at 02:47 PM..
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-26-2010, 04:41 PM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Did you decide that I'm not worth the trouble or effort?
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-27-2010, 11:04 AM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Wow, you must have....

Anywho - for anyone else who's interested in helping me with this... I decided to do something a little differently...

I no longer get any errors that show up, however, I'm not getting any data either...

Any thoughts or suggestions for a guy who's just three weeks into learning C#?

Code:
protected void loadRepeaterGrid(String TicketNumber)
        {
        //load the grid with the comments of the ticket(s)

        //create the SQL Connection
        SqlConnection sqlConn_Comments = new SqlConnection(ConfigurationManager.ConnectionStrings["My_ConnectionString"].ConnectionString);

        //Create the SQL Command
        SqlCommand sqlCmdComments = new SqlCommand("Get_TicketComments", sqlConn_Comments);
        sqlCmdComments.CommandType = CommandType.StoredProcedure;
        
        //Add the parameters needed for the query
        sqlCmdComments.Parameters.Add(new SqlParameter("@TixNumber", TicketNumber));

        //Open the connection to the DB
        sqlConn_Comments.Open();

        //Read the data
        SqlDataReader dr_Comments = sqlCmdComments.ExecuteReader();

        dr_Comments.Read();

        //create a repeater
        Repeater Repeater1 = new Repeater();

        //If AddedBy is null, then populate accordingly
        if (dr_Comments.IsDBNull(dr_Comments.GetOrdinal("AddedBy")) || (dr_Comments["AddedBy"].ToString()==""))
            {
            foreach (RepeaterItem repeateItem in Repeater1.Items)
                {
                if (repeateItem.ItemIndex == 0)
                    {
                    //Create the repeater's data source
                    Repeater1.DataSource = dr_Comments;
                    //bind the data to the repeater
                    Repeater1.DataBind();

                    RepeaterItem headerItem = new RepeaterItem(repeateItem.ItemIndex, ListItemType.Header);
                    HtmlGenericControl hTag = new HtmlGenericControl("h4");
                    hTag.InnerHtml = "&nbsp;";
                    repeateItem.Controls.Add(hTag);

                    Label lbl = new Label();
                    RepeaterItem container = (RepeaterItem)lbl.NamingContainer;
                    String strVals = Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "TicketStatus")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "Comments"));
                    lbl.Text = strVals;
                    }
                }
            }
        else
            {
            foreach (RepeaterItem repeateItem in Repeater1.Items)
                {
                if (repeateItem.ItemIndex == 0)
                    {

                    //Create the repeater's data source
                    Repeater1.DataSource = dr_Comments;
                    //bind the data to the repeater
                    Repeater1.DataBind();

                    RepeaterItem headerItem = new RepeaterItem(repeateItem.ItemIndex, ListItemType.Header);
                    HtmlGenericControl hTag = new HtmlGenericControl("h4");
                    hTag.InnerHtml = "&nbsp;";
                    repeateItem.Controls.Add(hTag);

                    Label lbl = new Label();
                    RepeaterItem container = (RepeaterItem)lbl.NamingContainer;
                    String strVals = Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "TicketStatus")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "AddedBy")) + " - " + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "DateAdded")) + "<br />" + Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem, "Comments"));
                    lbl.Text = strVals;
                    }
                }
            }
        
        //add the repeater to the panel
        Panel1.Controls.Add(Repeater1);

        //close and dispose...
        dr_Comments.Close();
        dr_Comments.Dispose();
        sqlConn_Comments.Close();
        sqlConn_Comments.Dispose();

        }
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-27-2010, 12:14 PM Re: C# Reading From Database - Need to find and use special characters.
Extreme Talker

Posts: 246
Trades: 0
You are trying to loop through the items in the repeater before you even bind the dr_Comments as the DataSource. This needs to be done first, then you can loop through the items as there are no items until you bind the datasource. You can also verify that the datasource is being filled by going into debug mode and setting a break point and seeing if anything is being returned.

Also look at using ItemTemplates for your repeater.

http://www.asp101.com/articles/john/...er/default.asp
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
View Public Profile
 
Old 04-27-2010, 01:41 PM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
You are absolutely correct, but I did that in the beginning... sort of (first post - through 12th post) and got either this error:
Quote:
"Object reference not set to an instance of an object."
and/or this error (depending on how I phrased the IF statement)
Quote:
"Invalid attempt to read when no data is present."
Perhaps, I may be mistaken? how about an example of what you mean...?
Like I said, I'm still 3 weeks into learning this language for work... and I'd really like to understand the "how" as well as the "why"...

(also asking for patience)
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 04-28-2010, 02:14 PM Re: C# Reading From Database - Need to find and use special characters.
Extreme Talker

Posts: 246
Trades: 0
This example should have everything you need to setup a repeater properly (with the added code for collapsing rows)

http://www.4guysfromrolla.com/demos/...eRepeater.aspx
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
View Public Profile
 
Old 04-28-2010, 03:09 PM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
...not quite what I'm looking for, but cool as hell.

I'm going to have to bookmark that for another project that I have coming up.


Thanks, though.
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Old 05-10-2010, 01:57 PM Re: C# Reading From Database - Need to find and use special characters.
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
just in case anyone is wondering, i decided to do something differently and it seemed to work for what I wanted to do.

Thanks for whatever input you gave.
__________________
Need a vacation.
mb2000inc is offline
View Public Profile Visit mb2000inc's homepage!
 
Closed Thread     « Reply to C# Reading From Database - Need to find and use special characters.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 3.05352 seconds with 12 queries