Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,639 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 964 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



looping, arrays and hyperlinks, oh my!

 
Reply to this topicStart new topic

looping, arrays and hyperlinks, oh my!

lldietrich
post 26 Jun, 2008 - 08:43 AM
Post #1


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 6

Okay...so here is my first question:

I am allowing users on my page to upload files. That much works. In the first form, all they see are labels listing the files that have been uploaded for a particular record.

On the next stage of the form, I need to list all the files that have been uploaded as hyperlinks. They will all be .doc files.

I am not sure where I should put the code to loop through the variable that contains the file names (delimited by ";") and I am not sure how to list them as hyperlinks.

Here is what I have:
CODE

#region LoadRecord()
        private void LoadRecord(string RecNum)
        {
            LoadGrid();
            GetAvailableFlags();
            Panel1.Visible=false;
            Panel2.Visible=true;
            DataTable dt=ds.Tables[0];
            string filterExp = "";
            filterExp = "RecNum="+RecNum;
            DataRow[] rowFound = dt.Select(filterExp);
            
            foreach (DataRow dr in rowFound)
            {
                ID.Text=dr["RecNum"].ToString();
                lblRecNum.Text=dr["RecNum"].ToString();
                DTG_Submit.Text = dr["DTG_Submit"].ToString();
                LoginID.Text = dr["LoginID"].ToString();
                PIN.Text = dr["PIN"].ToString();
                StartDate.Text = dr["StartDate"].ToString();
                StartTime.Text = dr["StartTime"].ToString();
                Title.Text = dr["Title"].ToString();
                Details.Text = dr["Details"].ToString();
                HelpDesk.SelectedValue = dr["HelpDesk"].ToString();
                CurrentScreenShot.Text= dr["ScreenShot"].ToString();
            }    
        }
        #endregion

#region SubmitData()
        private bool SubmitData()
        {
            

            CUsers user =new CUsers();
            user=(CUsers)Session["SessionUser"];

            int iInvLenght=Convert.ToInt16(NewInvestigateLength.Text);
    
            if(NewInvestigation.Text.Length>iInvLenght)
            {
                Investigate.Text=CurrentComments.Text + NewInvestigation.Text;
            }
            else
                Investigate.Text=CurrentComments.Text + strAdd;
        
            if(CurrentScreenShot.Text.Length > 0)
            {
                if(NewScreenShot.Text.Length > 3)
                {
                    ScreenShot.Text=CurrentScreenShot.Text + "; " + NewScreenShot.Text;
                }
                else
                    ScreenShot.Text=CurrentScreenShot.Text;
            }
            else
                ScreenShot.Text= NewScreenShot.Text;
            
            
            
            int RecNum=Convert.ToInt16(ID.Text);  //ORIGINAL
            
            bool bNoError =true;
            SqlConnection con = new SqlConnection(strConnection);
            string strSQL;
            DataSet ds = new DataSet();
            
            user=(CUsers)Session["SessionUser"];

            if(user.Role == "QC" || user.Role == "TL" || user.Role == "DMO" || user.Role == "DM" ||                    user.Role == "QC_AI" || user.Role == "Web-based DM" || user.Role == "Test Officer")
            {
                strSQL = "SELECT [RecNum],[DTG_Submit],[LoginID],[Test], "+
                    "[PIN],[StartDate],[StartTime],[Title],[Details], "+                        "[HelpDesk],[ScreenShot]"+
                    "FROM Some_Report "+
                    "WHERE RecNum ="+RecNum;
            }
            else
            {
                strSQL="Select RecNum, Title, Details from Some_Report WHERE RecNum ="+                    RecNum;
            }                    
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(strSQL,con);                    
                da.Fill(ds);
                DataTable mytable = ds.Tables[0];
                DataRow myRow;
                myRow = mytable.Rows[0];

                string n;
                
                foreach(DataColumn c in mytable.Columns)
                {
                    n = c.ColumnName;

                    if(n == "RecNum")
                    {
                        continue;
                    }
                    myRow[n] = ControlTypeCheck.CheckControlType(Page.FindControl(n),n);
                
                }
                SqlCommandBuilder cb = new SqlCommandBuilder(da);
                da.Update(ds);
            }
            catch(Exception err)
            {
                string str=err.Message;
                bNoError=false;
            }
            finally
            {
                con.Close();
            }
                    
            return bNoError;
        }
        #endregion

        private void uploaddata()
        {    
            
            int RecNum =Convert.ToInt16(lblRecNum.Text);
            bool bNoError =true;
            SqlConnection con = new SqlConnection(strConnection);
            string strSQL;
            DataSet ds = new DataSet();
            CUsers user =new CUsers();
            user=(CUsers)Session["SessionUser"];
            
            if(user.Role=="QC"||user.Role=="TL"||user.Role=="DMO")

            {
                strSQL="UPDATE Some_Report set ScreenShot='"+NewScreenShot.Text+"' and '"+CurrentScreenShot.Text+"' where RecNum ="+RecNum;
            }
        
        }

        private void btnUpload_Click(object sender, System.EventArgs e)
        {                        
            if(NewFile.PostedFile!=null)
            {
                try
                {
                    string serverFileName;
                    
                    serverFileName=Path.GetFileName(NewFile.PostedFile.FileName);
//                    NewFile.PostedFile.SaveAs(@"D:\Web\Databases\Test\Documents\ScreenShots\"+serverFileName);
                    NewScreenShot.Text= serverFileName;

                    uploaddata();

                }
                catch(Exception err)
                {
                    string s = err.Message;
                }
            
            }
        
        }



The variable I need to loop through is the "ScreenShot" variable. Depending on the LoginID and Record Number, I need to list the files that have been uploaded as hyperlinks on the page.

I hope this makes sense, if not, just let me know what else or what other information would be helpful to you.

User is offlineProfile CardPM

Go to the top of the page


djkitt
post 27 Jun, 2008 - 09:34 AM
Post #2


D.I.C Head

**
Joined: 22 May, 2008
Posts: 116



Thanked 13 times
My Contributions


Hey there,

You can split the string into a string array like so:
CODE

        string[] s2 = ScreenShot.Split(';');



then you can foreach through s2 and make up some htmls code like so:

CODE

        StringBuilder sb = new StringBuilder();
        foreach (string s3 in s2)
        {
            sb = sb.Append("<a href=\"" + s3 + "\" >" + s3 + "</a><br />");
        }


then you can set up a div on your page and add the html using InnerHtml like so:
CODE

        screenshotholder.InnerHtml = sb.ToString();
.
.
.
                  <!-- This goes in your page -->
                  <div id="screenshotholder" runat="server"> </div>


Or you could use Response.Write to send your html string back to the document...

Anyway, hope that helps.
User is offlineProfile CardPM

Go to the top of the page

lldietrich
post 27 Jun, 2008 - 11:20 AM
Post #3


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 6

QUOTE(djkitt @ 27 Jun, 2008 - 09:34 AM) *

Hey there,

You can split the string into a string array like so:
CODE

        string[] s2 = ScreenShot.Split(';');



then you can foreach through s2 and make up some htmls code like so:

CODE

        StringBuilder sb = new StringBuilder();
        foreach (string s3 in s2)
        {
            sb = sb.Append("<a href=\"" + s3 + "\" >" + s3 + "</a><br />");
        }


then you can set up a div on your page and add the html using InnerHtml like so:
CODE

        screenshotholder.InnerHtml = sb.ToString();
.
.
.
                  <!-- This goes in your page -->
                  <div id="screenshotholder" runat="server"> </div>


Or you could use Response.Write to send your html string back to the document...

Anyway, hope that helps.


Thanks so much for the reply. I will give this a shot and let you know how it turns out!

~Lori
User is offlineProfile CardPM

Go to the top of the page

lldietrich
post 1 Jul, 2008 - 06:00 AM
Post #4


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 6

Hi Kitt,

I wanted to let you know that I got your code to work, the only problem is: the links don't go to the right path.
I added the path to where the screenshots would be like this:
CODE


sb = sb.Append("<a href=../Documents/ScreenShots/"+ s3 + " >" + s3 + "</a><br />");



When running in debug mode, it lists the correct files as filling s3, but when the page displays and I click on the links, the path returns: C:/Test/Documents/ScreenShots/" and if there is another file, that one has the same path except instead of a double quote, it has the first word in the file.

What I need to be able to do is display the links in another window. If it was HTML I know I could use _blank, but I am not sure how to do it in C#.

Thanks for all your help. I hope you can give me some insight as to why I am not getting the full path to the files.

~Lori
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 1 Jul, 2008 - 06:34 AM
Post #5


D.I.C Head

**
Joined: 22 May, 2008
Posts: 116



Thanked 13 times
My Contributions


QUOTE(lldietrich @ 1 Jul, 2008 - 08:00 AM) *

Hi Kitt,

I wanted to let you know that I got your code to work, the only problem is: the links don't go to the right path.
I added the path to where the screenshots would be like this:
CODE


sb = sb.Append("<a href=../Documents/ScreenShots/"+ s3 + " >" + s3 + "</a><br />");



When running in debug mode, it lists the correct files as filling s3, but when the page displays and I click on the links, the path returns: C:/Test/Documents/ScreenShots/" and if there is another file, that one has the same path except instead of a double quote, it has the first word in the file.

What I need to be able to do is display the links in another window. If it was HTML I know I could use _blank, but I am not sure how to do it in C#.

Thanks for all your help. I hope you can give me some insight as to why I am not getting the full path to the files.

~Lori


Hey Lori,

First off, you *are* generating html here, so you are right that target="_blank" is the way to open the links in another window. I see that I typed 'htmls code' and if that is the source of your confusion I am sorry. It was just a typo, I meant 'html code'.

So if you want this to open in a new window just add that like so...
CODE

sb = sb.Append("<a href=\"" + s3 + "\" target=\"_blank\" >" + s3 + "</a><br />");


On to your problem with the path/filename not working out right:

I use the backslash character to generate a quote inside the string so that the link will look like this:
<a href="fred.doc" target="_blank">fred.doc</a>
instead of this:
<a href=fred.doc target=_blank>fred.doc</a>

That is the way I personally like my html code to look, but I think yours will execute just as well...

Anyway, I suppose it is possible that the quote is coming from a \" that is left in your code or something...

I think I would suggest examining sb in debug mode and maybe you can see what is going on. Or you could break this into two steps with a string s4, like so and examine what is happening in both s4 and sb...

CODE

        foreach (string s3 in s2)
        {
            string s4 = "<a href=\"" + s3 + "\" >" + s3 + "</a><br />";
            sb = sb.Append(s4);
        }


If you are still having a hard time finding the issue post a little more of your code and I will take a look at it.

It looks like you are just about there.

Good luck,






User is offlineProfile CardPM

Go to the top of the page

lldietrich
post 1 Jul, 2008 - 11:49 AM
Post #6


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 6



Me again blink.gif

Okay, I am now able to have the documents list in a seperate window. I copied the contents of sb and ScreenshotHolder and they are as follows:
CODE

m_StringValue    "<a href=../Documents/ScreenShots/Test document for testing file upload.doc target=\"_blank\" >Test document for testing file upload.doc</a><br /><a href=../Documents/ScreenShots/DLS test.doc target=\"_blank\" >DLS test.doc</a><br />"    string


-ScreenshotHolder    {InnerText="<a href=../Documents/ScreenShots/Test document for testing file upload.doc target=\"_blank\" >Test document for testing file upload.doc</a><br /><a href=../Documents/ScreenShots/DLS test.doc target=\"_blank\" >DLS test.doc</a><br />"}    System.Web.UI.HtmlControls.HtmlGenericControl


I also copied the paths that each document is returning:

CODE

http://localhost/DLS/Documents/ScreenShots/"Test

http://localhost/DLS/Documents/ScreenShots/"DLS


Here is the exact code I am using:
CODE

if(CurrentScreenShot.Text!=null)
                {
                    string ScreenShot = CurrentScreenShot.Text;
                    string [] s2 = ScreenShot.Split(';');
                    StringBuilder sb = new StringBuilder();
                
                    foreach (string s3 in s2)
                    {
                        sb = sb.Append("<a href=../Documents/ScreenShots/" + s3 + " target=\"_blank\" >" + s3 + "</a><br />");
                    }
                        ScreenshotHolder.InnerHtml = sb.ToString();
                }


And on the html page:
CODE

<TR>
                                    <TD colSpan="2">Current uploaded files for this record:<BR>
                                        <asp:Label id="CurrentScreenShot" Runat="server"></asp:Label><BR>
                                        <DIV id="ScreenshotHolder" Runat="server"></DIV>
                                    </TD>
                                </TR>


?? I don't know what the problem is, seems I should be getting the correct path, but I am not. I don't know if version of .net make a difference to what I am doing here, but I am using .net 1.1 and Visual Studio 2003. I really appreciate your help Kitt and I am SOOOO glad I found this community of resources. I have tried others with no luck, this is the first tangible help I have gotten. So, again I would like to thank you. Let me know if I need to post anything else.

~Lori
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 2 Jul, 2008 - 10:35 AM
Post #7


D.I.C Head

**
Joined: 22 May, 2008
Posts: 116



Thanked 13 times
My Contributions




OK,

Thanks.

First, if you have spaces in your file names I think you need to enclose them in quotes the same way you are enclosing the _blank value for target. I believe that is why you are only seeing the first word in the file name when you look at a shortcut.

I don't know why the quotation mark is showing up between the path and the file name.

Could you post the value of CurrentScreenShot before you split it?




User is offlineProfile CardPM

Go to the top of the page

lldietrich
post 2 Jul, 2008 - 11:59 AM
Post #8


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 6

QUOTE(djkitt @ 2 Jul, 2008 - 10:35 AM) *

OK,

Thanks.

First, if you have spaces in your file names I think you need to enclose them in quotes the same way you are enclosing the _blank value for target. I believe that is why you are only seeing the first word in the file name when you look at a shortcut.

I don't know why the quotation mark is showing up between the path and the file name.

Could you post the value of CurrentScreenShot before you split it?


Kitt,

Here is the value of CurrentScreenShot:

CODE

-    CurrentScreenShot    {Text="Test document for testing file upload.doc;DLS test.doc"}    System.Web.UI.WebControls.Label


also, here is what I am getting from the links...the quotation mark is gone:

CODE

http://localhost/DLS/Documents/ScreenShots/Test

http://localhost/DLS/Documents/ScreenShots/DLS


Before sending this to you, I did as you suggested and enclosed the file names in quotes and it worked, it is now giving me the full path to the document AND displaying the document in a seperate window! Thanks so much! This has really been the best! I sincerely appreciate all of your help.

~Lori

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/29/08 06:49PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month