Search This Blog

Friday, July 30, 2010

Code to format string in datalist column


   1:<asp:Label ID="lblcreateddateval" runat="server" Text='<%#Convert.ToDateTime(Eval("CreatedDate")).ToShortDateString() %>'>
</asp:Label> 

Thursday, July 29, 2010

Sql query to get substring of column value

suppose i have column value i.e ../Images/photo.jpeg
But i want that my store procedure will return only Images/photo.jpeg
For this i can write a query in store procedure

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




ALTER PROCEDURE [dbo].[csp_GetUserGallery]

AS
BEGIN
   

    Select   top 6 REPLACE (PhotoLink, '../' , ' ' ) as PhotoLink from HomePhotoGallery where Activegallery=1 order by PhotoID
END

Here photolink is column name in table and HomePhotoGallery is table name
Here i use top6 keyword that will return only 6 top six records of table arranged by Photoid

Code to get datakey value from the datalist on Button click which is Outside datalist

  Source code for datalist

   1:     <asp:DataList ID="newsgallerylist" runat="server" RepeatDirection="Horizontal" RepeatColumns="6" DataKeyField="PhotoID">
   2:     <ItemTemplate>
   3:             <table> 
   4:                <tr>
   5:                  <td>
   6:    <asp:CheckBox ID="Imagecheck" runat="server" AutoPostBack="true" Checked='<%#Eval("ActiveGallery") %>'/>
   7:                td>
   8:                <td >
   9:   <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("PhotoLink") %>'/>
  10:              td>
  11:     tr>
  12:           table>
  13:   
  14:  ItemTemplate>
  15:                     
  16:  asp:DataList>
 
C# code to get datakey from datalist at runtime
 

   1:  public void getdatakey()
   2:          {
   3:              ArrayList photoid = new ArrayList();
   4:              ArrayList UpdatePhoto = new ArrayList();
   5:              CheckBox ch1;
   6:              Int32 mem_id = 0;
   7:              for (int j = 0; j < newsgallerylist.Items.Count; j++)
   8:              {
   9:                  ch1 = (CheckBox)(newsgallerylist.Items[j].FindControl("Imagecheck"));
  10:                  if (ch1.Checked == true)
  11:                  {
  12:                      mem_id = Convert.ToInt32(newsgallerylist.DataKeys[j]);
  13:                      photoid.Add(mem_id);
  14:                  }
  15:                  else
  16:                  {
  17:                      mem_id = Convert.ToInt32(newsgallerylist.DataKeys[j]);
  18:                      UpdatePhoto.Add(mem_id);
  19:     
  20:                  }
  21:              }
  22:              foreach (int id in photoid)
  23:              {
  24:                  BusinessObjects.clsHomePhotoGallery objphotoid = new BusinessObjects.clsHomePhotoGallery();
  25:                  objphotoid.updateAdminphotos(id);
  26:              }
  27:             foreach (int id in UpdatePhoto)
  28:              {
  29:                  BusinessObjects.clsHomePhotoGallery objphotoid1 = new BusinessObjects.clsHomePhotoGallery();
  30:                  objphotoid1.updateAdminphotos1(id);
  31:   
  32:              }
  33:          }