I am having an SQL Server database, which is having a lots of table.
Each of the table is having a primary column, which in fact is a GUID column.
I have created new ASP.NET Dynamic data site to manage these tables. The problem is that, the GUIDs are not being generated and that's why the site breaks for all tables.
I tried by adding "newID()" in SQL table, but it still fails.
I believe I need to somehow do changes in code. I believe this might be an issue faced by many developers working with ASP.Net Dynamic site.. Any idea of how to fix this?
I think I cheated but I just created a new GUID Field template.
To use create the extension metadata for your class. Shortened version shown here. And associate it with your class.
Then add the GUID.ascx and GUID_Edit.ascx to your project. When you insert a new record a GUID will be generated and show up read only in your form. Key is making the UIHint to be a GUID type.
Code below...
--- EventCalendarMetadata.cs
namespace DFEventsWeb
{
[MetadataType( typeof ( EventCalendar_Metadata ) ) ]
public partial class EVENT_CALENDAR
{
[DisplayName("EVENTS")]
private abstract class EventCalendar_Metadata
{
[Display(Name = "ID")]
[UIHint("GUID")]
public object EVENT_ID { get; set; }
.... other fields removed ...
}
}
}
--- GUID.ascx
<%@ Control Language="C#" CodeBehind="GUID.ascx.cs" Inherits="DFEventsWeb.GUID" %>
<asp:Literal runat="server" ID="Literal1" Text="<%# FieldValueString %>" />
--- GUID.ascx.cs
namespace DFEventsWeb
{
public partial class GUID : System.Web.DynamicData.FieldTemplateUserControl
{
private const int MAX_DISPLAYLENGTH_IN_LIST = 100;
public override string FieldValueString
{
get
{
string value = base.FieldValueString;
if (ContainerType == ContainerType.List)
{
if (value != null && value.Length > MAX_DISPLAYLENGTH_IN_LIST)
{
value = value.Substring(0, MAX_DISPLAYLENGTH_IN_LIST - 3) + "...";
}
}
return value;
}
}
public override Control DataControl
{
get
{
return Literal1;
}
}
}
}
--- GUID_Edit.ascx
<%@ Control Language="C#" CodeBehind="GUID_Edit.ascx.cs" Inherits="DFEventsWeb.GUID_EditField" %>
<asp:Literal ID="Literal1" runat="server" Text='<%# OVERRIDE_GUID %>'></asp:Literal>
--- GUID_Edit.ascx.cs
namespace DFEventsWeb
{
public partial class GUID_EditField : System.Web.DynamicData.FieldTemplateUserControl
{
const int MAX_TEXT_COLUMN_SIZE = 200; // jeh
string m_GUIDString;
public string OVERRIDE_GUID
{
get { return m_GUIDString; }
set { m_GUIDString = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
OVERRIDE_GUID = ( FieldValueEditString.Length <= 0 ? System.Guid.NewGuid().ToString() : FieldValueEditString );
}
protected override void ExtractValues(IOrderedDictionary dictionary)
{
dictionary[Column.Name] = ConvertEditedValue(Literal1.Text);
}
public override Control DataControl
{
get
{
return Literal1;
}
}
}
}
I have added following code in "Inserting" event of a FormView Control in ~PageTemplates\Insert.aspx.
e.Values.Add("Id", Guid.NewGuid());
As all my tables have a consistent naming convension as Id represents PK column, I am fine to use this hard-coded column name, or else, you may need to write appropriate logic to find PK column.