Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public string CorrectedPicture(object container)
       {
           object devNumber = DataBinder.Eval(container, "ImageFileName");
           string ret = devNumber.ToString();

           //// Add picture link if needed.
           DataRow row = ((DataRowView)container).Row;
           object ImageFileName = row["ImageFileName"];

           string ImageFileName1 = ImageFileName.ToString();
           string[] values = ImageFileName1.Split(',');

           for (int i = 0; i <= 0; i++)
           {


               if (ImageFileName != DBNull.Value)
               {
                   string url = @"D:\NewOutlookFiles" + txtProjectId.Text + "/" + values[i].ToString();
                   string titleText = "Deviation" + values[i].ToString() + "Picture";
                   string link = "javascript: void openPicture('_" + values[i].ToString() + "', '" + titleText + "', '" + url + "')";
                   ret = "<a href=\x22" + link + "\x22>" + ret + "</a>";
               }
           }

           return ret;
       }




<asp:Label ID="lblPicture" runat="server" Text='<%# this.CorrectedPicture(Container.DataItem) %>'
ItemStyle-wrap="true" ItemStyle-Width="10px">


What I have tried:

My "ImageFileName" column has multiple values separated by comma , and binding values to label. it's working fine but problem is i want to bind only first value to the label not all the values from column
Posted
Updated 12-Jul-16 3:07am
Comments
Richard Deeming 12-Jul-16 9:54am    
What is the purpose of the for loop? The code within it will only run once.

Instead running a loop just assign the first value in the array after you did the split.
To get the first value from the "ImageFileName", you can do something like following-
C#
string imgfilename= ImageFileName1.Split(',')[0];

If you are suspecting of possible empty entries or a comma in the start of the string, you can make use of StringSplitOption[^] to prevent such values as result.
C#
string imgfilename= ImageFileName1.Split(',',StringSplitOptions.RemoveEmptyEntries)[0];


Hope, it helps.
Please let me know in case you need further help on this.

Thanks :)
 
Share this answer
 
try this

do the split after validating null and dbnull values

C#
object ImageFileName = row["ImageFileName"];
           if (ImageFileName != DBNull.Value && ImageFileName != null )
           {
               string value = ImageFileName.ToString().Split(',')[0];
               string url = @"D:\NewOutlookFiles" + txtProjectId.Text + "/" + value;
               string titleText = "Deviation" + value + "Picture";
               string link = "javascript: void openPicture('_" + value + "', '" + titleText + "', '" + url + "')";
               ret = "<a href=\x22" + link + "\x22>" + ret + "</a>";
           }
 
Share this answer
 
Quote:
My "ImageFileName" column has multiple values separated by comma , and binding values to label. it's working fine but problem is i want to bind only first value to the label not all the values from column
Split and select the first one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900