Insert statement does not insert any data - asp.net

I have a page where I display a list of groups for my application. There can be multiple users in a group. The group list is implemented with a gridview. Besides the column for the group description, the gridview has a button for adding users to it. When I click this button, the groupid is passed to a new page, on this page there is another gridview with a list of users. The gridview also has a buttonfield for adding that specific user to the group's group id. For this I have an extra table:
UserGroup
usergroupid
userid
groupid
My problem is, nothing is inserted into this table, the insert command is executed but something seems to be missing.
Here is my code:
Markup:
<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:GridView OnRowCommand="grdBenutzer_RowCommand" ID="grdBenutzer" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField ReadOnly="true" DataField="BenutzerID" HeaderText="ID" />
<asp:TemplateField HeaderText="Bezeichnung">
<ItemTemplate>
<%# Eval("Bezeichnung")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList AppendDataBoundItems="true" runat="server" ID="ddwnBezeichnung" Text='<%# Bind("Bezeichnung")%>'>
<asp:ListItem Text="Mitarbeiter" Value="Mitarbeiter"></asp:ListItem>
<asp:ListItem Text="Praktikant" Value="Praktikant"></asp:ListItem>
<asp:ListItem Text="Azubi" Value="Azubi"></asp:ListItem>
<asp:ListItem Text="Umschüler" Value="Umschüler"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vorname">
<ItemTemplate>
<%# Eval("Vorname")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtVorname" Text='<%# Bind("Vorname")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nachname">
<ItemTemplate>
<%# Eval("Nachname")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtNachname" Text='<%# Bind("Nachname")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField ReadOnly="true" HeaderText="Geburtsdatum" DataField="Geburtsdatum" />
<asp:TemplateField HeaderText="Benutzerart">
<ItemTemplate>
<%# Eval("Benutzerart")%>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="Mitglied hinzufügen" Text="Mitglied hinzufügen" CommandName="MitgliedHinzufuegen" />
</Columns>
</asp:GridView>
<asp:SqlDataSource OnInserted="SqlDataSource1_Inserted" ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [BenutzerID], [Bezeichnung], [Vorname], [Geburtsdatum], [Nachname], [Benutzerart] FROM [Benutzer] WHERE [Archiviert] != 1"
InsertCommand="INSERT INTO BenutzerGruppe (BenutzerID, GruppenID) VALUES (#BenutzerID, #GruppenID)">
</asp:SqlDataSource>
</asp:Content>
Code-behind:
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
Response.Redirect("Gruppenverwaltung.aspx");
}
protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MitgliedHinzufuegen")
{
string gruppenid = Request.QueryString["GruppenID"];
SqlDataSource1.InsertParameters.Add("GruppenID", gruppenid);
SqlDataSource1.Insert();
}
}
The inserted method fires after I add an user to the group, but when I view the group's members, no user was added....

The insert command expects input parameter #BenutzerID which you don't appear to be providing in the code-behind.

Related

Setting EditIndex on a asp:GridView, can I be sure it is the correct row after rebinding to live data has been done?

Having an asp:GridView, when I put it into edit mode by setting EditIndex, I also need to update its data using the DataSource property and call DataBind like this:
protected void GrdFoo_OnRowEditing(object sender, GridViewEditEventArgs e)
{
grdFoo.EditIndex = e.NewEditIndex;
grdFoo.DataSource = DataBase.GetFoos();
grdFoo.DataBind();
}
Now, if the database contents was changed since I was populating the gridview first, how can I be sure I will edit the correct row?
edit
I got the suggestion to use OnRowDataBound. I am using a command field to trigger GrdFoo_OnRowEditing. Is the use of OnRowDataBound still a good idea?
Here is how I use the CommandField (aspx):
<asp:GridView ID="grdFoo" runat="server"
OnRowEditing="GrdFoo_OnRowEditing"
OnRowCancelingEdit="GrdFoo_OnRowCancelingEdit"
OnRowUpdating="GrdFoo_OnRowUpdating"
OnRowDeleting="GrdFoo_OnRowDeleting"
DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label Text='<%#Eval("Name") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%#Eval("Name") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label Text='<%#Eval("Email") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" Text='<%#Eval("Email") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"
ShowDeleteButton="True" buttontype="Image"
DeleteImageUrl="delete.png"
EditImageUrl="edit.png"
CancelImageUrl="cancel.png"
UpdateImageUrl="save.png"/>
</Columns>
</asp:GridView>

can't access asp controls from gridview

I have user control with a gridview, but for some reason I can't use the asp controls from within the backend code.
In the other user controls I can use them normally but at this particular user control it gives me error when I am trying to use a control which is used in the gridview.
Here is the code for my gridview, please let me know if you notice something unusual:
<asp:GridView ID="gvGDG" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1" Width="100%" CssClass="mGrid" GridLines="None" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
AllowPaging="True" >
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ItemStyle-HorizontalAlign="Center" CausesValidation="False">
</asp:CommandField>
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True"
ItemStyle-HorizontalAlign="Center" >
</asp:BoundField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" DataTextField="name" DataValueField="ID" onselectedindexchanged="ddlcountry_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" />
</Triggers>
</asp:UpdatePanel>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="State Province" SortExpression="State_Province">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("State_Province") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSource3" SelectedValue='<%# Bind("State_Province") %>'
DataTextField="StateName" DataValueField="StateName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [StateName] FROM [States]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="GDG Type" SortExpression="State_Province">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("GDG_Type") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlGdg" runat="server" DataSourceID="SqlDataSource4" SelectedValue='<%# Bind("GDG_Type") %>'
DataTextField="GDG" DataValueField="GDG">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [GDG] FROM [GDG]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Energy Type UOM" SortExpression="Energy_Type_UOM">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Energy_Type_UOM") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUomEnergy" runat="server" DataSourceID="SqlDataSource5" SelectedValue='<%# Bind("Energy_Type_UOM") %>'
DataTextField="UOM" DataValueField="UOM">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT UOM FROM UOM WHERE (Type = 'E')"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="GDG UOM" SortExpression="GDG_UOM">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("GDG_UOM") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUom" runat="server" DataSourceID="SqlDataSource6" SelectedValue='<%# Bind("GDG_UOM") %>'
DataTextField="UOM" DataValueField="UOM">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT UOM FROM UOM WHERE (Type = 'O')"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="GDG_Coeficient_Value"
HeaderText="GDG Coeficient Value" SortExpression="GDG Coeficient Value"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
Thanks in advance, Laziale
If I m right then. You have a User Control. Say wucMyControl.ascx, where you have a gridview control gvGDG. And u are unable to access it from wucMyControl.ascx.cs. If its the problem then use the solution below:
In your user controls designer file check the access level of the Gridview control. Might be it will get changed to private, accidently , instead of protected. If its private , make it protected or public and check whether you are able to access it now or not.
Update
FYI, you can't access directly the child controls. For this you need to access in the controls DataBound event.
Here in your case :
protected void GridView_RowDataBound(sender, e)
{
if(e.Row.RowType == DataControlRowtype.DataRow)
{
var ddl = (DropdownList)e.Row.FindControl("ddlCountry");
// do whatever you want with ddl. Similarly you can find all html and server control inside a gridview.
}
}
This will help you...
Adding event to gridview child controls
In your GridView1_RowDataBound method, add the handler when you create the drop down by calling ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
Then, declare the void ddl_SelectedIndexChanged(object sender, EventArgs e) method to handle your logic. The sender argument will be a reference to the drop down's that was selected. Also set AutoPostback property of the drodownlist to true.
I think maybe you have some wrong with code on top line of page ASP.net designer:
Please check on CodeFile="??????" and Inherits="????????"

GridView Control

I have a grid control. And I want to access gridview child control from other controls.
Ex:
<asp:UpdatePanel ID="upPersonelAssignment" runat="server">
<ContentTemplate>
<asp:Label ID="lblPersonelName" runat="server" ></asp:Label>
<asp:Label ID="lblUpdatedDateTime" runat="server" ></asp:Label>
<div id="divPersonelAssignmentSearch">
<asp:GridView ID="gvPersonelAssignment" runat="server" DataSourceID="odsPersonelBLL"
AllowSorting="True" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:CommandField CancelText="Vazgeç" EditText="Amir Ata" ShowEditButton="True"
UpdateText="Amir Kaydet" />
<asp:TemplateField HeaderText="Sicil No">
<ItemTemplate>
<asp:Label ID="lblPersonelSicilNo" runat="server" Text='<%# Eval("personelSicil") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="İsim">
<ItemTemplate>
<asp:Label ID="lblPesonelFirstName" runat="server" Text='<%# Eval("personelAdi") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField> ...
...........
<asp:TemplateField HeaderText="Amir Liste">
<ItemTemplate>
<asp:Label ID="lblAmirList" runat="server" Text="Amir Liste"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...........
<div id="divDropDownExtender" runat="server">
<act:DropDownExtender ID="ddeAmirList" runat="server"
TargetControlID="lblAmirList" DropDownControlID="">
</act:DropDownExtender>
</div>
But I have error : The TargetControlID of 'ddeAmirList' is not valid. A control with ID 'lblAmirList' could not be found
How to access grid view child control from other control?
using the ondatabound even of the gridview
you can access the control inside your gridview by writing the following:
protected void YourGridView_DataBound(object sender, EventArgs e)
{
Label label1 = (Label)e.Row.Cells[0].FindControl("lblAmirList");
// access your control
label1.Text = "sdfs";
// do this for other controls inside gridview
}
You have to put extenders within the GridView control for this to work, within the template of the control.

gridview control problem

work on C# asp.net vs05. i take a combo on gridview template field.
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" />
<asp:TemplateField HeaderText="DivisionName" SortExpression="DivisionName">
<ItemTemplate>
<asp:Label ID="lblDivisionName" runat="server" Text='<%# Bind("DivisionName") %>'
Width="116px"></asp:Label><br />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentName" SortExpression="StudentName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("StudentName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Update" />
</Columns>
</asp:GridView>
Now i want to fill this combo by the bellow code ?
DropDownList1.DisplayMember = "CommercialRegionName";
foreach (object oItem in collection)
{
DropDownList1.Items.Add(oItem);
}
how to get the combo control id from the grid
You can use the following code:
Set OnRowDataBound of the gridview in aspx as OnRowDataBound="GridView3_RowDataBound"
Then put the following code in aspx.cs page.
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList1 =(DropDownList) e.Row.FindControl("DropDownList1");
DropDownList1.DisplayMember = "CommercialRegionName";
foreach (object oItem in collection)
{
DropDownList1.Items.Add(oItem);
}
}
}

Simple databinding to gridview columns

I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).

Resources