Hi, Here is the Gridview
<asp:GridView ID="gv2" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="rec_id" DataSourceID="SqlDataSource2"
ondatabound="gv2_DataBound" Width="232px" onrowcommand="gv2_RowCommand">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="rec_id" HeaderText="rec_id" InsertVisible="False"
ReadOnly="True" SortExpression="rec_id" />
<asp:BoundField DataField="user_id" HeaderText="user_id"
SortExpression="user_id" />
<asp:BoundField DataField="int_desc" HeaderText="Description"
SortExpression="int_desc" />
<asp:BoundField DataField="change_date" HeaderText="Edit On"
SortExpression="change_date" />
</Columns>
<EmptyDataTemplate>
No Record Found
</EmptyDataTemplate>
</asp:GridView>
Here are few events to enable Insert when there is no records in the Gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
tb_join_dt.Text = DateTime.Now.GetDateTimeFormats('d')[0];
}
private void BindGridView()
{
DataTable dt = new DataTable();
dt = (DataTable)Session["SqlDataSource2"];
//gv2.DataSource = GetDataSource();
gv2.DataBind();
But I see only "No record Found" in that when running the project. Why is there no prompt for data input? How to make sure the Gridview is ready for data input?
select your gv. klick ">" to open gridview task menu then click edittemplate. If you have several template, make sure to choose emptydatatemplate. Drag fromview to that template and configure the datasource using wizard. Dont forget to check autogenerate
insert/update/delete option.
Server Error in '/app2' Application.
--------------------------------------------------------------------------------
Both DataSource and DataSourceID are defined on 'gv2'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'gv2'. Remove one definition.
Source Error:
Line 29: //dt = (DataTable)Session["SqlDataSource2"];
Line 30: gv2.DataSource = GetDataSource();
Line 31: gv2.DataBind();
Line 32:
Line 33: }
[InvalidOperationException: Both DataSource and DataSourceID are defined on 'gv2'. Remove one definition.]
System.Web.UI.WebControls.DataBoundControl.EnsureSingleDataSource() +12465015
System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +56
System.Web.UI.WebControls.DataBoundControl.GetData() +9
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +131
app2._Default.BindGridView() in C:\Setup\app2\Default.aspx.cs:31
app2._Default.Page_Load(Object sender, EventArgs e) in C:\Setup\app2\Default.aspx.cs:22
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
with these events
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
tb_join_dt.Text = DateTime.Now.GetDateTimeFormats('d')[0];
}
private void BindGridView()
{
//DataTable dt = new DataTable();
//dt = (DataTable)Session["SqlDataSource2"];
gv2.DataSource = GetDataSource();
gv2.DataBind();
wmec
Contributor
6224 Points
3221 Posts
No prompt for data input
Jan 31, 2013 03:58 AM|LINK
Hi,
Here is the Gridview
<asp:GridView ID="gv2" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="rec_id" DataSourceID="SqlDataSource2"
ondatabound="gv2_DataBound" Width="232px" onrowcommand="gv2_RowCommand">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="rec_id" HeaderText="rec_id" InsertVisible="False"
ReadOnly="True" SortExpression="rec_id" />
<asp:BoundField DataField="user_id" HeaderText="user_id"
SortExpression="user_id" />
<asp:BoundField DataField="int_desc" HeaderText="Description"
SortExpression="int_desc" />
<asp:BoundField DataField="change_date" HeaderText="Edit On"
SortExpression="change_date" />
</Columns>
<EmptyDataTemplate>
No Record Found
</EmptyDataTemplate>
</asp:GridView>
Here are few events to enable Insert when there is no records in the Gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
tb_join_dt.Text = DateTime.Now.GetDateTimeFormats('d')[0];
}
private void BindGridView()
{
DataTable dt = new DataTable();
dt = (DataTable)Session["SqlDataSource2"];
//gv2.DataSource = GetDataSource();
gv2.DataBind();
}
protected DataTable GetDataSource()
{
const string key = "SqlDataSource2";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("rec_id", typeof(string));
dt.Columns.Add("user_id", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Edit On", typeof(string));
DataColumn[] keys = new DataColumn[4];
keys[0] = dt.Columns["rec_id"];
dt.PrimaryKey = keys;
dt.Rows.Add("1", "1");
dt.Rows.Add("2", "1");
dt.Rows.Add("3", "third object");
dt.Rows.Add("4", Convert.ToString(DateTime.Now));
Session[key] = dt;
}
return dt;
}
But I see only "No record Found" in that when running the project. Why is there no prompt for data input? How to make sure the Gridview is ready for data input?
HuaMin Chen
oned_gk
All-Star
31495 Points
6428 Posts
Re: No prompt for data input
Jan 31, 2013 07:36 AM|LINK
You can place any control inside EmptyDataTemplate
Simple way is add formview (insermode as default) to EmptyDataTemplate.
ReBind the GV in the formview ItemInserted event.
Or place another control what you want like griview to insert data.
wmec
Contributor
6224 Points
3221 Posts
Re: No prompt for data input
Jan 31, 2013 08:07 AM|LINK
Do you mean to have a formview inside EmptyDataTemplate? Can I have sample/more details for what you suggested?
HuaMin Chen
oned_gk
All-Star
31495 Points
6428 Posts
Re: No prompt for data input
Jan 31, 2013 10:28 AM|LINK
Yes
simply create fromview and sqldatasource inside the template, dont forget to set default mode to insert mode.
Use design mode and wizard
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DefaultMode="Insert" oniteminserted="FormView1_ItemInserted"> </asp:FormView>protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) { GridView1.DataBind(); }wmec
Contributor
6224 Points
3221 Posts
Re: No prompt for data input
Feb 01, 2013 01:14 AM|LINK
Do you mean to have formview inside this
<EmptyDataTemplate>
...
</EmptyDataTemplate>
on the page? How about the details of the steps to do this in Design mode?
HuaMin Chen
oned_gk
All-Star
31495 Points
6428 Posts
Re: No prompt for data input
Feb 01, 2013 01:46 AM|LINK
select your gv. klick ">" to open gridview task menu then click edittemplate. If you have several template, make sure to choose emptydatatemplate. Drag fromview to that template and configure the datasource using wizard. Dont forget to check autogenerate insert/update/delete option.
wmec
Contributor
6224 Points
3221 Posts
Re: No prompt for data input
Feb 01, 2013 02:12 AM|LINK
Many thanks. Here are the gridview and formview
<asp:GridView ID="gv2" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="rec_id" DataSourceID="SqlDataSource2"
ondatabound="gv2_DataBound" Width="232px" onrowcommand="gv2_RowCommand">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="rec_id" HeaderText="rec_id" InsertVisible="False"
ReadOnly="True" SortExpression="rec_id" />
<asp:BoundField DataField="user_id" HeaderText="user_id"
SortExpression="user_id" />
<asp:BoundField DataField="int_desc" HeaderText="Description"
SortExpression="int_desc" />
<asp:BoundField DataField="change_date" HeaderText="Edit On"
SortExpression="change_date" />
</Columns>
<EmptyDataTemplate>
No Record Found
<asp:FormView ID="FormView1" runat="server" DataKeyNames="rec_id"
DataSourceID="SqlDataSource1" oniteminserted="FormView1_ItemInserted">
<EditItemTemplate>
rec_id:
<asp:Label ID="rec_idLabel1" runat="server" Text='<%# Eval("rec_id") %>' />
<br />
user_id:
<asp:TextBox ID="user_idTextBox" runat="server" Text='<%# Bind("user_id") %>' />
<br />
int_desc:
<asp:TextBox ID="int_descTextBox" runat="server"
Text='<%# Bind("int_desc") %>' />
<br />
change_date:
<asp:TextBox ID="change_dateTextBox" runat="server"
Text='<%# Bind("change_date") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
user_id:
<asp:TextBox ID="user_idTextBox" runat="server" Text='<%# Bind("user_id") %>' />
<br />
int_desc:
<asp:TextBox ID="int_descTextBox" runat="server"
Text='<%# Bind("int_desc") %>' />
<br />
change_date:
<asp:TextBox ID="change_dateTextBox" runat="server"
Text='<%# Bind("change_date") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
rec_id:
<asp:Label ID="rec_idLabel" runat="server" Text='<%# Eval("rec_id") %>' />
<br />
user_id:
<asp:Label ID="user_idLabel" runat="server" Text='<%# Bind("user_id") %>' />
<br />
int_desc:
<asp:Label ID="int_descLabel" runat="server" Text='<%# Bind("int_desc") %>' />
<br />
change_date:
<asp:Label ID="change_dateLabel" runat="server"
Text='<%# Bind("change_date") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Mssqlconn2 %>"
DeleteCommand="DELETE FROM [tab3] WHERE [rec_id] = @rec_id"
InsertCommand="INSERT INTO [tab3] ([user_id], [int_desc], [change_date]) VALUES (@user_id, @int_desc, @change_date)"
SelectCommand="SELECT * FROM [tab3]"
UpdateCommand="UPDATE [tab3] SET [user_id] = @user_id, [int_desc] = @int_desc, [change_date] = @change_date WHERE [rec_id] = @rec_id">
<DeleteParameters>
<asp:Parameter Name="rec_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="int_desc" Type="String" />
<asp:Parameter Name="change_date" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="int_desc" Type="String" />
<asp:Parameter Name="change_date" Type="DateTime" />
<asp:Parameter Name="rec_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Mssqlconn2 %>"
DeleteCommand="DELETE FROM [tab3] WHERE [rec_id] = @rec_id"
InsertCommand="INSERT INTO [tab3] ([user_id], [int_desc], [change_date]) VALUES (@user_id, @int_desc, @change_date)"
SelectCommand="SELECT * FROM [tab3]"
UpdateCommand="UPDATE [tab3] SET [user_id] = @user_id, [int_desc] = @int_desc, [change_date] = @change_date WHERE [rec_id] = @rec_id">
<DeleteParameters>
<asp:Parameter Name="rec_id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="int_desc" Type="String" />
<asp:Parameter Name="change_date" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="int_desc" Type="String" />
<asp:Parameter Name="change_date" Type="DateTime" />
<asp:Parameter Name="rec_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
...
Here is one event
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
gv2.DataBind();
}
...
but when running the page, I don't see any prompt for data input. If you need I can also send you the table definition, for you to verify it.
HuaMin Chen
wmec
Contributor
6224 Points
3221 Posts
Re: No prompt for data input
Feb 01, 2013 06:23 AM|LINK
Dear all,
Any help to this?
HuaMin Chen
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: No prompt for data input
Feb 02, 2013 01:22 AM|LINK
Hi,
As far as I see, I see your codes has something logic wrong:
1) Your session's name must be type of string, you've missed the pair of "".
2) You should set DataSource to a data contents.
3)
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView(); } tb_join_dt.Text = DateTime.Now.GetDateTimeFormats('d')[0]; } private void BindGridView() { gv2.DataSource = GetDataSource(); gv2.DataBind(); } protected DataTable GetDataSource() { const string key = "SqlDataSource2"; DataTable dt = Session["key"] as DataTable; if (dt == null) { dt = new DataTable(); dt.Columns.Add("rec_id", typeof(string)); dt.Columns.Add("user_id", typeof(string)); dt.Columns.Add("Description", typeof(string)); dt.Columns.Add("Edit On", typeof(string)); DataColumn[] keys = new DataColumn[1]; keys[0] = dt.Columns["rec_id"]; dt.PrimaryKey = keys; dt.Rows.Add("1", "1"); dt.Rows.Add("2", "1"); dt.Rows.Add("3", "third object"); dt.Rows.Add("4", Convert.ToString(DateTime.Now)); Session["key"] = dt; } return dt; }wmec
Contributor
6224 Points
3221 Posts
Re: No prompt for data input
Feb 02, 2013 02:58 AM|LINK
Many thanks Decker. I do encounter this.
Server Error in '/app2' Application.
--------------------------------------------------------------------------------
Both DataSource and DataSourceID are defined on 'gv2'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'gv2'. Remove one definition.
Source Error:
Line 29: //dt = (DataTable)Session["SqlDataSource2"];
Line 30: gv2.DataSource = GetDataSource();
Line 31: gv2.DataBind();
Line 32:
Line 33: }
Source File: C:\Setup\app2\Default.aspx.cs Line: 31
Stack Trace:
[InvalidOperationException: Both DataSource and DataSourceID are defined on 'gv2'. Remove one definition.]
System.Web.UI.WebControls.DataBoundControl.EnsureSingleDataSource() +12465015
System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +56
System.Web.UI.WebControls.DataBoundControl.GetData() +9
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +131
app2._Default.BindGridView() in C:\Setup\app2\Default.aspx.cs:31
app2._Default.Page_Load(Object sender, EventArgs e) in C:\Setup\app2\Default.aspx.cs:22
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
with these events
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
tb_join_dt.Text = DateTime.Now.GetDateTimeFormats('d')[0];
}
private void BindGridView()
{
//DataTable dt = new DataTable();
//dt = (DataTable)Session["SqlDataSource2"];
gv2.DataSource = GetDataSource();
gv2.DataBind();
}
protected DataTable GetDataSource()
{
const string key = "SqlDataSource2";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("rec_id", typeof(string));
dt.Columns.Add("user_id", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Edit On", typeof(string));
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns["rec_id"];
dt.PrimaryKey = keys;
dt.Rows.Add("1", "1");
dt.Rows.Add("2", "1");
dt.Rows.Add("3", "third object");
dt.Rows.Add("4", Convert.ToString(DateTime.Now));
Session["key"] = dt;
}
return dt;
}
...
HuaMin Chen