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.
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") %>'...
So I've got a gridview bound to a SQL datasource stored procedure. The stored procedure returns a pivot table, where a number of columns are unknown until run time. I therefore need to autogenerate the columns.
The gridview is intended to allow Excel-like data updates. So the goal is to have the grid load as textboxes. I know if I were to declare the columns on my aspx page, I could create template columns and handle their visibility that way.
So I believe I need a way to programmatically set all the columns to template fields (without knowing the column names), or I need to discover the method by which I can just "flip the switch" and just make everything editable.
Thanks in advance!
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSourceExcelGridTest">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceExcelGridTest" runat="server" ConnectionString="<%$ ConnectionStrings:WebAppsConnectionString %>" SelectCommand="spJobForecastingGetEmployeesByDepartmentAndProject_v1" SelectCommandType="StoredProcedure" UpdateCommand="spJobForecastingUpdateHours" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="DepartmentNumber" Type="String" DefaultValue="13211" />
<asp:Parameter Name="ProjectNumber" Type="String" DefaultValue="13211" />
<asp:Parameter Name="startDate" Type="DateTime" DefaultValue="2/21/2014" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DepartmentNumber" Type="String" />
<asp:Parameter Name="ProjectNumber" Type="String" />
<asp:Parameter Name="Alias" Type="String" />
<asp:Parameter Name="WorkWeek" Type="DateTime" />
<asp:Parameter Name="WorkHours" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
try this:
UPDATED CODE
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSourceExcelGridTest">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceExcelGridTest" runat="server" ConnectionString="<%$ ConnectionStrings:WebAppsConnectionString %>" SelectCommand="spJobForecastingGetEmployeesByDepartmentAndProject_v1" SelectCommandType="StoredProcedure" UpdateCommand="spJobForecastingUpdateHours" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:Textbox ID="DepartmentNumber" DefaultValue="13211" text='<%# Eval("DepartmentNumber") %>'></asp:TextBox>
<asp:Textbox ID="ProjectNumber" Type="String" DefaultValue="13211" Text='<%# Eval("ProjectNumber") %>'></asp:TextBox>
<asp:Textbox ID="startDate" Type="DateTime" DefaultValue="2/21/2014" Text='<%# Eval("startDate") %>'></asp:TextBox>
</SelectParameters>
<UpdateParameters>
<asp:Textbox ID="DepartmentNumber" Type="String" text='<%# Eval("DepartmentNumber") %>'></asp:TextBox>
<asp:Textbox ID="ProjectNumber" Type="String" text='<%# Eval("ProjectNumber") %>'></asp:TextBox>
<asp:Textbox ID="Alias" Type="String" text='<%# Eval("Alias") %>'></asp:TextBox>
<asp:Textbox ID="WorkWeek" Type="DateTime" text='<%# Eval("WorkWeek") %>'></asp:TextBox>
<asp:Textbox ID="WorkHours" Type="Int32" text='<%# Eval("WorkHours") %>'></asp:TextBox>
</UpdateParameters>
</asp:SqlDataSource>
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 am working on a shopping cart project for my college project in final page of my cart i want to calculate the total amount for the all the product in the cart help me in that code for that is
for cart.aspx
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="id"
DataSourceID="SqlDataSource1"
EmptyDataText="No Item in the Cart">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="pName"
HeaderText="pName"
SortExpression="pName" />
<asp:BoundField DataField="brand"
HeaderText="brand"
SortExpression="brand" />
<asp:TemplateField HeaderText="img"
SortExpression="img">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("img") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1"
runat="server"
ImageUrl='<%# Bind("img") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="quantity"
HeaderText="quantity"
SortExpression="quantity" />
<asp:BoundField DataField="price"
HeaderText="price"
SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<% $ConnectionStrings:shopingConnectionString1 %>"
DeleteCommand="DELETE FROM [completeCart] WHERE [id] = #id"
InsertCommand="INSERT INTO [completeCart] ([uName], [pName], [brand], [img], [quantity], [price]) VALUES (#uName, #pName, #brand, #img, #quantity, #price)"
SelectCommand="SELECT * FROM [completeCart] WHERE ([uName] = #uName)"
UpdateCommand="UPDATE [completeCart] SET [uName] = #uName, [pName] = #pName, [brand] = #brand, [img] = #img, [quantity] = #quantity, [price] = #price WHERE [id] = #id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
</InsertParameters>
<SelectParameters>
<asp:CookieParameter CookieName="uname"
Name="uName"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
</asp:SqlDataSource>
for code behind file
protected void Page_Load(object sender, EventArgs e)
{
string s2 = System.Web.HttpContext.Current.User.Identity.Name;
Response.Cookies["uname"].Value = s2;
}
finally the result i want is the total sum of cost of product display on a label control on the same page
Remove the SqlDataSource control. Don't put SQL in aspx page! Create a class that returns cart and calculates grand total. Databind in code behind.
I am thinking of that your cart.aspx page has a grid view for displaying the cart items and prices , then you can do like this....
you can do like this ... Simply in GridView.RowDataBound Event loop gridview and find control contain price amount and sum them
decimal grdTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));
grdTotal = grdTotal + rowTotal;
}
lbl.Text = grdTotal.ToString("c");
}
}
you can display total price value at the footer of price column .......
<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.