<asp:TabPanel ID="AbsentTabPanel" runat="server" HeaderText="Excused Absences">
<ContentTemplate>
<asp:ListView ID="AbsentListView" runat="server" InsertItemPosition="LastItem"
DataSourceID="AbsentSqlDataSource" DataKeyNames="">
<InsertItemTemplate>
<tr>
<td>
<asp:DropDownList ID="ExcusedAbsences_employee_idDropDownList2" runat="server">
<asp:ListItem Text="John Smith" Value="1" />
<asp:ListItem Text="Indiana Jones" Value="2" />
</asp:DropDownList>
</td>
<td>
<!-- start date -->
<asp:TextBox ID="start_dt_codeTextBox" runat="server" Columns="6" Text='<%#Bind("start_dt")%>' />
</td>
<td>
<!-- end date -->
<asp:TextBox ID="end_dt_codeTextBox" runat="server" Columns="6" Text='<%#Bind("end_dt")%>' />
</td>
<td>
<asp:Button runat="server" CommandName="Insert" Text="insert" />
<asp:Button runat="server" CommandName="Clear" Text="clear" />
</td>
</tr>
</InsertItemTemplate>
...
<asp:SqlDataSource ID="AbsentSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:Rotations3ConnectionString %>"
SelectCommand="SELECT employee_id, start_dt, end_dt
FROM Excused_Absences
WHERE fy = #fy AND pgy = #pgy"
UpdateCommand="UPDATE Excused_Absences
SET start_dt = #start_dt, end_dt = #end_dt, employee_id = #employee_id
WHERE absence_nbr = #absence_nbr"
InsertCommand="INSERT INTO Excused_Absences (fy, pgy, employee_id, start_dt, end_dt)
VALUES (#fy, #pgy, #employee_id, #start_dt, #end_dt)"
OldValuesParameterFormatString="old_{0}">
<SelectParameters>
<asp:ControlParameter Name="fy" Type="Int32" ControlID="fyTextBox" PropertyName="Text" />
<asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter Name="fy" Type="Int32" ControlID="fyTextBox" PropertyName="Text" />
<asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
<asp:Parameter Name="employee_id" Type="String" />
<asp:Parameter Name="start_dt" Type="DateTime" />
<asp:Parameter Name="end_dt" Type="DateTime" />
</InsertParameters>
For some reason when I insert John Smith or Indiana Jones, the employee_id is not inserted into the database, I get a NULL value. What am I missing?
Make the employee_id parameter a ControlParameter like the pgy parameter. The controlID should be the DropDownlist ID.
<asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
<asp:Parameter Name="employee_id" Type="String" />
Or, you could manually set the parameter to a value.
Found it! long time programmer, but novice to asp.net so I'm not sure of the reason, but just poking around other code that did the same thing i wanted, I was able to write this function:
Protected Sub AbsentListView_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles AbsentListView.ItemInserting
e.Values("employee_id") = DirectCast(AbsentListView.InsertItem.FindControl("ExcusedAbsences_employee_idDropDownList2"), DropDownList).SelectedValue
End Sub
I couldn't find any call to this function, but apparnetly it gets called automatically with every insert of my "AbsentlistView" item.
This function somehow tells the SQL command what the value should be for the drop down, and therefore when the SQL command goes, it has the correct value in the employee_id field.
Thanks all for your help.
I guess you need to bind the ExcusedAbsences_employee_idDropDownList2 SelectedValue property to the employee_ID field.
Related
I need help to understand the proper way to map or associate TemplateFields to the ObjectDataSource. I show only one (of many) template definitions here:
<asp:TemplateField HeaderText="First Name" SortExpression="cFIRSTNAME">
<ItemTemplate>
<asp:Label ID="lblValFirstName" runat="server" Text='<%# Eval("cFIRSTNAME") %>' style="font-weight: bold;" ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("cFIRSTNAME") %>' MaxLength="20"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("cFIRSTNAME") %>' MaxLength="20"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
The ObjectDataSource and update parameters are:
<asp:ObjectDataSource ID="ODS_LOGIN_DETAILS" runat="server"
SelectMethod="GetLOGINSbyLoginID"
TypeName="bllLOGINS"
DeleteMethod="DeleteFromDetailsView"
InsertMethod="InsertFromDetailsView"
UpdateMethod="UpdateFromDetailsView" OldValuesParameterFormatString="original_{0}"> <UpdateParameters>
<asp:Parameter Name="p_UID_LOGIN" Type="Int32" />
<asp:Parameter Name="p_UID_CONTACT" Type="Int32" />
<asp:Parameter Name="p_UID_USER_TYPE" Type="Int32" />
<asp:Parameter Name="p_TXT_USERNAME" Type="String" />
<asp:Parameter Name="p_TXT_PASSWORD" Type="String" />
<asp:Parameter Name="p_BOOL_IS_ACTIVE" Type="Boolean" />
<asp:Parameter Name="p_DT_END" Type="DateTime" />
<asp:Parameter Name="p_FirstName" Type="String" />
<asp:Parameter Name="p_LastName" Type="String" />
<asp:Parameter Name="p_CONTACTTITLE" Type="String" />
<asp:Parameter Name="p_TXT_PHONE" Type="String" />
<asp:Parameter Name="p_CONTACT_EMAIL" Type="String" /> </UpdateParameters>
What is NOT clear to me is how is the mapping made from the templatefield textbox (txtFirstName) to the UpdateParameter "p_FirstName"?
In the existing code I am working with, I see the ObjectDataSource_Updating()-event that does a series of DVW.FindControl() values being assigned/set to the
e.InputParameter("param-name") = text-box-value
Questions?
Is this done in either the ObjectDataSource_Updating()-event or in the DetailsView_Updtaing()-event -or- by declaration aspx code?
or how?
What is the best way to do this?
Would naming the textbox-ID and the parameter-name simplify the code?
I am assuming the solutions offered would be "similar" or the same as with SqlDataSource -- please confirm?
Thanks in advance...John
I had a similar problem and was unsure how to map the textbox to the params as was getting NULL param value passed when doing a SQL trace. In the end I set the parameter name to match the value in the Text attribute bind which worked.
<UpdateParameters>
<asp:Parameter Name="my_field" Type="String" Direction="Input" />
...
<asp:TextBox Text='<%# Bind("my_field") %>'...
We have a DropDownList in the markup of an ASP.Net / VB.Net web form.
We are wanting to populate the DropDownList with data from a DataSet created from the DataSet designer but the coding we are using in the code-behind file does not find the DropDownList ID using FindControl.
Can you check my coding and let me know what I still need to do to get the DropDownList populated?
Markup of the DropDownList:
<% '-- DetailsView (Grid) for details of the GridView -- %>
<% '---------------------------------------------------- %>
<asp:DetailsView
ID="DetailsView"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="207px"
DataSourceID="SqlDataSourceDetails"
DataKeyNames="ID"
OnItemCommand="DetailsViewDetails_ItemCommand"
OnDataBound="DetailsView_DataBound">
<Fields>
<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListClass" Runat="server"> </asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEditDropDownListClass" runat="server"
ControlToValidate="DropDownListClass"
ErrorMessage="Please select a class." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="LiteralClass" runat="server"
Text='<%# FormatAsMixedCase(Eval("ClassName").ToString())%>' />
</ItemTemplate>
<ItemStyle ForeColor="Blue" />
</asp:TemplateField>
</Fields>
Coding in the code-behind file:
Protected Sub DetailsView_DataBound(sender As Object, e As EventArgs)
Dim theClassesTableAdapter As New DataSetClassesTableAdapters.ClassesTableAdapter
Dim ddlTheDropDownList = DirectCast(FindControl("DropDownListClass"), DropDownList)
ddlTheDropDownList.DataSource = theClassesTableAdapter.GetDataByAllClasses
ddlTheDropDownList.DataTextField = "ClassName"
ddlTheDropDownList.DataValueField = "ClassID"
ddlTheDropDownList.SelectedValue = "ClassID"
ddlTheDropDownList.DataBind()
End Sub
Markup of the DataSouce of the DetailsView:
<% '-- Datasources -- %>
<% '----------------- %>
<asp:SqlDataSource
ID="SqlDataSourceDetails"
runat="server"
ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>"
DeleteCommand=
"DELETE FROM [TeacherSchedule]
WHERE [ID] = #ID"
InsertCommand=
"INSERT INTO [TeacherSchedule]
([DayOfWeek],
[Grade],
[StartTime],
[EndTime],
[ClassID])
VALUES (#DayOfWeek,
#Grade,
#StartTime,
#EndTime,
#ClassID)"
SelectCommand=
"SELECT TeacherSchedule.ID, TeacherSchedule.Grade, TeacherSchedule.StartTime, TeacherSchedule.EndTime, TeacherSchedule.TeacherID, TeacherSchedule.ClassID,
TeacherSchedule.DayOfWeek, Classes.ClassName, Teachers.Forename, Teachers.Surname
FROM TeacherSchedule INNER JOIN
Classes ON TeacherSchedule.ID = Classes.ID INNER JOIN
Teachers ON TeacherSchedule.ID = Teachers.ID
WHERE (TeacherSchedule.ID = #ID)"
UpdateCommand=
"UPDATE [TeacherSchedule]
SET [DayOfWeek] = #DayOfWeek,
[Grade] = #Grade,
[StartTime] = #StartTime,
[EndTime] = #EndTime,
[ClassID] = #ClassID
WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="DayOfWeek" Type="String" />
<asp:Parameter Name="Grade" Type="String" />
<asp:Parameter Name="StartTime" Type="String" />
<asp:Parameter Name="EndTime" Type="String" />
<asp:Parameter Name="ClassID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridViewSummary" Name="ID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DayOfWeek" Type="String" />
<asp:Parameter Name="Grade" Type="String" />
<asp:Parameter Name="StartTime" Type="String" />
<asp:Parameter Name="EndTime" Type="String" />
<asp:Parameter Name="ClassID" Type="Int32" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
Try and populate your DropDownList in the DropDownList_Init event handler.
Markup:
<asp:DropDownList ID="ddlTheDropDownList" runat="server" OnInit="ddlTheDropDownList_Init">
The code behind should look something like this, I'm more used to C# but I hope you understand the point:
Protected Sub ddlTheDropDownList_Init(sender As Object, e As EventArgs)
Dim ddl As DropDownList
ddl = sender As DropDownList
ddl.Datasource = theClassesTableAdapter.GetDataByAllClasses
ddl.DataTextField = "ClassName"
ddl.DataValueField = "ClassID"
ddl.SelectedValue = "ClassID"
ddl.DataBind()
End Sub
I have the following Repeater on my page:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<div id="fullcommentheader">
<span class="fullname">
<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Name") %>'></asp:Literal></span>
<br />
<span class="fullbodytext">
<asp:Button ID="Button2" runat="server" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' Text="Delete" />
<asp:Literal ID="LitBody2" Text='<%# Eval("Message")%>' runat="server"></asp:Literal></span>
<span class="dateTime">
<asp:Literal ID="Literal4" runat="server" Text='<%# Eval("CreateDateTime") %>'></asp:Literal></span>
</div>
<br />
</ItemTemplate>
</asp:Repeater>
I have a Button2 there that I'd like to use to delete the repeater entry, how do I query the database to achieve this? I'm used to have these commands by default on gridview, so I'm unsure on how to do this manually.
My event:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete" && e.CommandArgument.ToString() != "")
{
}
}
This is my SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>"
DeleteCommand="DELETE FROM [Comment] WHERE [Id] = #Id"
InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Message], [PostId]) VALUES (#Name, #Email, #Website, #Message, #PostId)"
SelectCommand="SELECT [Name], [Email], [Website], [Message], [PostId], [Id], [CreateDateTime] FROM [Comment] WHERE ([PostId] = #PostId)"
UpdateCommand="UPDATE [Comment] SET [Name] = #Name, [Email] = #Email, [Website] = #Website, [Message] = #Message, [PostId] = #PostId, [CreateDateTime] = #CreateDateTime WHERE [Id] = #Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Website" Type="String" />
<asp:Parameter Name="Message" Type="String" />
<asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
<asp:Parameter Name="CreateDateTime" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Website" Type="String" />
<asp:Parameter Name="Message" Type="String" />
<asp:Parameter Name="PostId" Type="Int32" />
<asp:Parameter Name="CreateDateTime" Type="DateTime" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
You will need to declare a private int _id global variable first.
Then, add the parameter dynamically in your sqldatasource deleting event:
Then in your ItemCommand 'delete', set the global _id to the CommandArgument since you passed that in. Then perform a SqlDataSource1.Delete()
_id = Convert.ToInt32(e.CommandArgument);
SqlDataSource1.Delete();
_id = 0;
//need to rebind your repeater here or you won't see the changes
protected void SqlDataSource1_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
//add the parameter
if (_id != 0)
e.Command.Parameters["#Id"].Value = _id;
}
Use the ItemCommand event for the repeater. This will trigger when the button is clicked and give you access to the command name and command argument. You can then use the command argument to delete the record from the database.
Do delete the record, you can use the SqlDataSource.Delete() method, but you will need to populate the delete parameter before calling that method or handle the Deleting event and populate the parameters there.
I have the following SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>"
InsertCommand="INSERT INTO [Chat] ([Username], [Message]) VALUES (#Username, #Message)"
SelectCommand="SELECT [Id], [Username], [Message], [Date] FROM [Chat] ORDER BY [Id]" >
<InsertParameters>
<asp:Parameter Name="Message" Type="String" />
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Username" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Which controls the following FormView:
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert"
OnItemInserted="fv_ItemInserted" RenderOuterTable="False"
DataSourceID="SqlDataSource1">
<InsertItemTemplate>
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server" CssClass="chattxtbox"
Text='<%# Bind("Message") %>' autocomplete="off"></asp:TextBox>
<asp:Button ID="Button1" runat="server" CommandName="insert" style="display:none" Text="Button" OnClick="insertUser"/>
</asp:Panel>
</InsertItemTemplate>
</asp:FormView>
I want to be able a variable's content into the Username column, so on Button1 I set the following event: OnClick="insertUser"
protected void insertUser(object sender, EventArgs e)
{
string username1 = User.Identity.Name;
SqlDataSource1.InsertParameters.Add("Username", username1);
SqlDataSource1.Insert();
}
This isn't working though, I don't get any SQL error message at all, is this the right way to do this? And where did I go wrong?
Change Insert Parameter to SessionParameter.
<InsertParameters>
<asp:Parameter Name="Message" Type="String" />
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Username" Type="String" />
<asp:SessionParameter Name="Username" SessionField="Username" Type="String" />
</InsertParameters>
And in Page_Load handler,
Session["Username"]=User.Identity.Name;
Here you want to declare your parameter as a Control Parameter and then you don't need to assign the value to the parameter in code. You can just call insert.
am using a reorder list in my UI. My reorder list is and SQLDataSource are shown below. I get an error message
"Reorder failed, see details below.\r\n\r\nFailed to reorder."
Seems trivial but not able to find the issue. Can you please help me with this issue ?
SOURCE CODE
' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT * FROM TBL_BATCH_STAGE WHERE RQUST_KEY=#RequestId ORDER BY [ORDER_NO] ASC"
UpdateCommand="UPDATE [TBL_BATCH_STAGE] SET [ORDER]=#Order WHERE [BATCH_STG_KEY] = #original_ID"
OldValuesParameterFormatString="original_{0}">
<SelectParameters>
<asp:SessionParameter Name="RequestId" SessionField="RequestId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Order" Type="Int32" />
<asp:Parameter Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Resolved this issue.My SQL parameters did not match my ReorderList variable names.
<asp:SqlDataSource ID="SqlDataSource1" OldValuesParameterFormatString="original_{0}" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT * FROM TBL_BATCH_STAGE WHERE RQUST_KEY=#RequestId ORDER BY [ORDER_NO] ASC"
UpdateCommand="UPDATE [TBL_BATCH_STAGE] SET [ORDER_NO]=#ORDER_NO WHERE [BATCH_STG_KEY] = #original_BATCH_STG_KEY" >
<SelectParameters>
<asp:SessionParameter Name="RequestId" SessionField="RequestId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ORDER_NO" Type="Int32" />
<asp:Parameter Name="original_BATCH_STG_KEY" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ReorderList ID="ReorderList1" runat="server" AllowReorder="true" DataKeyField="BATCH_STG_KEY" DataSourceID="SqlDataSource1" SortOrderField="ORDER_NO">
<ItemTemplate>
<table style="border:solid 1px #cccccc;">
<tr>
<td style="vertical-align:top;list-style:none;font-size:13px;padding:0 0 0 0;">
<asp:Label ID="ItemLabel" runat="server" Text='<%# Eval("BATCH_STG_TEXT")%>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:ReorderList>