18 Şubat 2013 Pazartesi

pictureBox usage (Telerik reports)

http://www.telerik.com/help/reporting/report-items-picture-box.html  explains how to use a pictureBox component pretty well. In my case, the photograph of the user is loaded to the pictureBox component dynamically. In order to accomplish this the first step is reading the photo from the photograph database:

1:  public static MemoryStream connectDbGetPhoto(string queryParam)  
2:      {  
3:        // open a connection to the database  
4:        SqlConnection dbCon = new SqlConnection(ConfigurationManager.ConnectionStrings[SauConstants.PHOTO_DATABASE].ConnectionString);  
5:        try  
6:        {  
7:          dbCon.Open();   
9:          StringBuilder query = new StringBuilder(" SELECT Resim from Resimler WHERE UserId = " + queryParam);  
10:          SqlDataAdapter adapter = new SqlDataAdapter(query.ToString(), dbCon);  
11:          DataSet ds = new DataSet();  
12:          adapter.Fill(ds);  
13:          DataTable dt = new DataTable();  
14:          dt = ds.Tables[0];  
15:          byte[] barrImg = (byte[])dt.Rows[0]["Resim"];  
16:          MemoryStream mstream = new MemoryStream(barrImg);  
17:          return mstream;  
18:        }  
19:        catch (Exception e)  
20:        {  
21:          return null;  
22:        }  
23:        finally  
24:        {  
25:          dbCon.Close();  
26:        }  
27:      }  

Then the image is loaded to PictureBox component as follows:

1:  MemoryStream resim = connectDbGetPhoto(user.ID.ToString());  
2:  this.picResim.Value = Image.FromStream(resim);  

However, if the loaded photograph is too big for the pictureBox, layout problems arise as in http://www.programmersheaven.com/mb/VBasic/322240/322240/display-whole-picture-in-picture-box-or-formplease-help/  To solve this problem:

1:  this.picResim.Sizing = ImageSizeMode.ScaleProportional;  

By the way, i use http://codeformatter.blogspot.com/ for a while to format my source code and i strongly recommend it!!

13 Şubat 2013 Çarşamba

workaround for error "The version of SQL Server in use does not support datatype 'datetime2' "

http://stackoverflow.com/questions/316422/using-sql-server-2008-and-sql-server-2005-and-date-time
explains the error pretty well.
In short, the workaround is opening the .edmx file with notepad, searching for phrase ProviderManifestToken and replacing 2008 with 2005..

ProviderManifestToken="2005"

11 Ocak 2013 Cuma

RadGrid: format double with eval

In order to format double with eval :
 <telerik:GridTemplateColumn HeaderText="Ortalama" DataField="Ortalama" UniqueName="Ortalama"  
                         SortExpression="Ortalama" HeaderStyle-Width="80px" >  
                         <ItemTemplate>  
                           <%# String.Format("{0:f2}", DataBinder.Eval(Container.DataItem, "Ortalama"))%>  
                         </ItemTemplate>  
                       </telerik:GridTemplateColumn>  
the solution is from: http://stackoverflow.com/questions/5168592/force-a-string-to-2-decimal-places

RadComboBox: custom sorting of items

The link (http://www.telerik.com/help/aspnet-ajax/combobox-how-to-implement-custom-sorting.html) explains how to implement a custom sorting mechanism for RadComboBox..
It says that Telerik RadComboBox sorts by using the text by default, but it seems to use the value instead..
In order to sort by text, the following class can be used:

1:  public class RadComboSortByText : IComparer  
2:    {  
3:      public int Compare(object x, object y)  
4:      {  
5:        RadComboBoxItem p1 = new RadComboBoxItem();  
6:        RadComboBoxItem p2 = new RadComboBoxItem();  
7:        if (x is RadComboBoxItem)  
8:          p1 = x as RadComboBoxItem;  
9:        if (y is RadComboBoxItem)  
10:          p2 = y as RadComboBoxItem;  
11:        int cmp = 0;  
12:        if (p1.ComboBoxParent.Sort == RadComboBoxSort.Ascending)  
13:        {  
14:          //here we compare the Text of the items  
15:          cmp = String.Compare(p1.Text, p2.Text, !p1.ComboBoxParent.SortCaseSensitive);  
16:        }  
17:        if (p1.ComboBoxParent.Sort == RadComboBoxSort.Descending)  
18:        {  
19:          //here we compare the Text of the items  
20:          cmp = String.Compare(p1.Text, p2.Text, !p1.ComboBoxParent.SortCaseSensitive) * -1;  
21:        }  
22:        return cmp;  
23:      }  
24:    }  
.. and itemBound of RadComboBox:
1:  protected void ComboEgitimDali_ItemDataBound(object sender, RadComboBoxItemEventArgs e)  
2:      {  
3:        ComboEgitimDali.Sort = RadComboBoxSort.Ascending;  
4:        ComboEgitimDali.SortItems(new RadComboSortByText());  
5:      }  
 <telerik:RadComboBox ID="ComboEgitimDali" runat="server" OnItemDataBound="ComboEgitimDali_ItemDataBound">  

10 Ocak 2013 Perşembe

RadGrid: to deselect the last selected item on client side

In order to deselect the last selected item in the RadGrid, the following function can be used on client side:
javascript:
 <telerik:RadCodeBlock ID="RadCodeBlock" runat="server">  
     <script type="text/javascript">  
       function DeselectLastSelected(gridID, index) {  
         var masterTable = $find(gridID).get_masterTableView();  
         masterTable.deselectItem(masterTable.get_selectedItems()[index].get_element());  
       }  
        function RowSelected(sender, eventArgs) {  
         if (sender != null) {  
           var numOfSelected = sender.get_masterTableView().get_selectedItems().length;  
           DeselectLastSelected(sender.ClientID, numOfSelected - 1);  
         }  
       }  
  </script>  
   </telerik:RadCodeBlock>  
RadGrid:
 telerik:RadGrid AutoGenerateColumns="False" ID="GridOgrenciDersListe" AllowFilteringByColumn="false"   
    AllowPaging="False" AllowSorting="False" runat="server" ShowGroupPanel="false"   
    Width="560px" AllowMultiRowSelection="True" ShowFooter="false" Visible="false">   
    <PagerStyle Mode="NextPrevAndNumeric" />   
    <GroupingSettings CaseSensitive="false" />   
    <ClientSettings EnableRowHoverStyle="true">   
     <Selecting AllowRowSelect="True" UseClientSelectColumnOnly="true"></Selecting>   
     <ClientEvents OnRowSelected="RowSelected" OnRowDeselected="RowDeselected" />   
    </ClientSettings>   
    <MasterTableView TableLayout="Fixed" DataKeyNames="ID" Font-Size="XX-Small" ClientDataKeyNames="ID">   
     <Columns>   
      <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn">   
      </telerik:GridClientSelectColumn>   
  </Columns>   
    </MasterTableView>   
   </telerik:RadGrid>