Our recent ASP.NET project has began to grow bigger and bigger, so yesterday I decided to grab all business logic from the project, separate to components and put them into class libraries. Mostly I used some automated copying process, but in ‘ClientManagerClassLibrary’ I had to recreate my ‘ClientProfile’ LINQ to SQL class using the built-in wizard in Visual Studio 2008 SP1 (…sure you know the process: Add new item -> LINQ to SQL class -> dropping the table and stored procedures into designer -> Save). After separating, the project still worked fine on my developer machine, not like on my server. I got this error:
SQL Server 2005 Error: “A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified) ”
I could not imagine what the hell is wrong with SQL Server. My connection string and SQL configuration the same on both machine and I didn’t modify that. So what else?
The solution have found in LINQ codebehinds.
<asp:ListView ID="livComments" runat="server" DataSourceID="CommentsObjectDataSource" EnableViewState="False" onitemcommand="livComments_ItemCommand" >
<ItemTemplate>
... /template here/ ...
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
<asp:DataPager ID="CommentDataPager" runat="server" PageSize="5">
<Fields>
<asp:TemplatePagerField OnPagerCommand="TemplatePagerField_OnPagerCommand">
<PagerTemplate>
<asp:Button ID="FirstButton" runat="server" CssClass="first" CommandName="First" Text="First" Enabled='<%# Container.StartRowIndex > 0 %>' />
<asp:Button ID="PreviousButton" runat="server" CommandName="Previous" Text='<%# (Container.StartRowIndex - Container.PageSize + 1).ToString() + " - " + (Container.StartRowIndex).ToString() %>' Visible='<%# Container.StartRowIndex > 0 %>' />
<!--<asp:Label ID="CurrentPageLabel" runat="server" Text='<%# (Container.StartRowIndex + 1).ToString() + "-" + ((Container.StartRowIndex + Container.PageSize > Container.TotalRowCount) ? (Container.TotalRowCount) : (Container.StartRowIndex + Container.PageSize)).ToString() %>' />-->
<asp:Button ID="NextButton" runat="server" CommandName="Next" Text='<%# (Container.StartRowIndex + Container.PageSize + 1).ToString() + " - " + ((Container.StartRowIndex + Container.PageSize*2 > Container.TotalRowCount) ? (Container.TotalRowCount) : (Container.StartRowIndex + Container.PageSize*2)).ToString() %>' Visible='<%# (Container.StartRowIndex + Container.PageSize) < Container.TotalRowCount %>' />
<asp:Button ID="LastButton" runat="server" CssClass="last" CommandName="Last" Text='Last' Enabled='<%# Container.StartRowIndex < (Container.TotalRowCount - Container.PageSize) %>'/>
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
</LayoutTemplate>
</asp:ListView>
You’ll get something like this:
