Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If No record found in child table for asp.net 2.0

Status
Not open for further replies.

silicon

Programmer
Aug 13, 2002
31
0
0
US
Hi,
In asp.net 2.0 i am using dropdownlist box,gridview and an detail view.
Master record are choosed from dropdown list box and an child record are displayed in gridview.On clicking of select button of gridview ,detailview is displayed with edit and new button.I had bind all three control using sqldatasource.
My problem is if there is no record in child table a gridview is not displayed.And i have no access to detailview.I want to insert a new record in detailview if there no child record .All three are controls are in same page.

Thanks in advance if any one had a solution for this prob.
Regards,
preet
 
I don't know which event i should use to invoke detailview when there is no record in child table
 
I don't know which event i should use to invoke detailview when there is no record in child table

What does that mean?

If there are no child records, then there will be no detail. I am not sure what you are trying to do.


Jim
 
Basicaly its an master detail senario where dropdown list contain master data,and on selection of dropdown list the details is displayed in gridview.Detailview is used to add or edit detail record.
If suppose on selection of dropdown list if no record found for that master record nothing is displayed below dropdown
ie nither gridview not detail view.In this case if suppose i want to add a new detail record to that master record what i should i do.
 
Hi,

Check the Rows.Count of the GridView after the DropDownList SelecedIndex has changed and the GridView bound.

e.g

If DS.Tables("Grid").Rows.Count = 0 Then
'show the add new item link etc.
Else
'do nothing
End If

Where DS.Tables("Grid") is your DataTable

j



 
If there is no corresponding rows, you would have to add a row to your datasource and bind it to your gridview.
 
I have used sqldatasource In that case how can i know the datatable name.
Pls. go through this code and give me some practical solution.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SweetHomeDBConnectionString %>"
SelectCommand="SELECT [Csrno], [Items] FROM [CategoryMst] ORDER BY [Items]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td style="width: 464px">
Select Category
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
DataTextField="Items" DataValueField="Csrno" Width="178px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList></td>
<td style="width: 612px">
&nbsp;</td>
</tr>
<tr>
<td colspan="2" >
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SweetHomeDBConnectionString %>"
SelectCommand="SELECT [CDsrno], [Csrno], [Items], [Description], [Qty], [Rate] FROM [CategoryDtl] WHERE ([Csrno] = @Csrno) ORDER BY [Items], [Qty]" OnSelected="SqlDataSource2_Selected">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="0" Name="Csrno" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td colspan="2" >
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="CDSrno" DataSourceID="SqlDataSource2"
ForeColor="#333333" GridLines="None" Width="706px" OnPageIndexChanged="GridView1_PageIndexChanged" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSorted="GridView1_Sorted">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" HeaderText="Operation" >
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:CommandField>
<asp:BoundField DataField="Items" HeaderText="Items" SortExpression="Items" NullDisplayText="NA" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" NullDisplayText="NA" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="60%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="60%" />
</asp:BoundField>
<asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" NullDisplayText="NA">
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="10%" />
<HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Rate" HeaderText="Rate" SortExpression="Rate" NullDisplayText="NA" >
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="10%" />
<HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="10%" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr >
<td colspan ="2" id="TD1">
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:SweetHomeDBConnectionString %>"
DeleteCommand="DELETE FROM [CategoryDtl] WHERE [CDsrno] = @CDsrno"
InsertCommand="INSERT INTO [CategoryDtl] ([Csrno], [Items], [Description], [Qty], [Rate]) VALUES (@Csrno, @Items, @Description, @Qty, @Rate)"
SelectCommand="SELECT d.[Csrno],m.[Items] as Category,d.[Items], d.[Description], d.[Qty], d.[Rate], d.[CDsrno] FROM [CategoryDtl] d,[CategoryMst] m WHERE ((d.[CDsrno] = @CDsrno) and (d.[Csrno]=m.[Csrno])) ORDER BY d.[Items]"
UpdateCommand="UPDATE [CategoryDtl] SET [Csrno] = @Csrno, [Items] = @Items, [Description] = @Description, [Qty] = @Qty, [Rate] = @Rate WHERE [CDsrno] = @CDsrno">
<DeleteParameters>
<asp:parameter Name="CDsrno" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="Csrno" Type="Int32" />
<asp:parameter Name="Items" Type="String" />
<asp:parameter Name="Description" Type="String" />
<asp:parameter Name="Qty" Type="Double" />
<asp:parameter Name="Rate" Type="Double" />
<asp:parameter Name="CDsrno" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="CDsrno" PropertyName="SelectedValue"
Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:parameter Name="Csrno" Type="Int32" />
<asp:parameter Name="Items" Type="String" />
<asp:parameter Name="Description" Type="String" />
<asp:parameter Name="Qty" Type="Double" />
<asp:parameter Name="Rate" Type="Double" />
</InsertParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td style="width: 464px">
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="CDsrno" DataSourceID="SqlDataSource3" ForeColor="#333333"
GridLines="None" Height="50px" Width="184%" OnDataBound="DetailsView1_DataBound" OnItemInserted="DetailsView1_ItemInserted" OnItemUpdated="DetailsView1_ItemUpdated">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#C5BBAF" Font-Bold="True" />
<EditRowStyle BackColor="#7C6F57" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="CDsrno" HeaderText="CDsrno" InsertVisible="False" ReadOnly="True"
SortExpression="CDsrno">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Csrno" HeaderText="Csrno" SortExpression="Csrno" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Csrno" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Items" HeaderText="Items" SortExpression="Items" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="Rate" HeaderText="Rate" SortExpression="Rate" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" >
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="50%" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="20%" />
</asp:CommandField>
</Fields>
<FieldHeaderStyle BackColor="#D0D0D0" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

This is code behind file


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void GridView1_Sorted(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.DataBind();
}
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
TextBox txtCategory = (TextBox)DetailsView1.Rows[2].Cells[1].Controls[0];
txtCategory.Text = DropDownList1.SelectedItem.Text;
txtCategory.Enabled = false;
}




}

protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.AffectedRows.Equals(0))
{
Response.Write("No Rows selected");

}
}

}
 
How can i find table name from my sqldatasource control
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top