Passing object to objectdatasource insert parameter - asp.net

This is my object data source code.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
InsertMethod="InsertCustomerItem" SelectMethod="GetCustomerItems"
TypeName="HPPWebsite.HPPClasses.TestClassHelper">
<InsertParameters>
<asp:Parameter Name="CustCode" Type="String" />
<asp:Parameter Name="ItemIn" Type="Object" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="CustCode" SessionField="CustomerCode"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
This is my code behind:
<protected void Button1_Click(object sender, EventArgs e)
{
TestClass item = new TestClass();
item.CustCode = Session["CustomerCode"].ToString();
item.ItemCode =" ";
item.BasePrice =0.0;
item.AddMarkUp =true;
ObjectDataSource1.InsertParameters["CustCode"].DefaultValue = item.CustCode;
ObjectDataSource1.InsertParameters.Add("itemin",TypeCode.Object,"item");
ObjectDataSource1.Insert();
}
It gives me an error that type string cannot be converted to my testclass object. How can this be resolved please? I can do it if i just pass the object fieldds as seperate parameters but im trying to pass the whole object and do the trimmings and calculations in the stored proc

Related

Send e-mail after update statement in ASPX

I'm building for my project work at school a website.
The main idea is that users can report damage or report a non functioning computer.
Then the IT department can give in if they've taken any steps try fixing it and there is also a checkbox if the problem is dealth with. If the checkbox in the formview is checked and updated, there should be sent an automatic e-mail to the original user with a message that the problem is solved. I've done everything and everything works, except for the e-mail part.
Can anybody give me some advice in how I should do this? There is an unique datasource who handeles the update statement.
Yours sincerely;
A.
Here is my code:
<asp:AccessDataSource ID="accAfgehandeld" runat="server"
DataFile="~/App_data/dbMeldingen.mdb"
DeleteCommand="DELETE FROM [tblMeldingen] WHERE [MeldingID] = ?"
InsertCommand="INSERT INTO [tblMeldingen] ([MeldingID], [Afgehandeld], [Categorie]) VALUES (?, ?, ?)"
SelectCommand="SELECT [MeldingID], [Afgehandeld], [Categorie] FROM [tblMeldingen] WHERE ([MeldingID] = ?)"
UpdateCommand="UPDATE [tblMeldingen] SET [Afgehandeld] = true, [Categorie] = ? WHERE [MeldingID] = ?"
OnUpdated="OnDSUpdatedHandler">
<DeleteParameters>
<asp:Parameter Name="MeldingID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="MeldingID" Type="Int32" />
<asp:Parameter Name="Afgehandeld" Type="Boolean" />
<asp:Parameter Name="Categorie" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="MeldingID" QueryStringField="id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Categorie" Type="Int32" />
<asp:Parameter Name="MeldingID" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
and my script in the same page:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="meldingendetails.ascx.vb" Inherits="meldingendetails" %>
<script runat="server">
private void OnDSUpdatedHandler(Object sender, e.Updated) {
if (e.AffectedRows > 0)
{
MailMessage mailMessage = new MailMessage();
SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
smtpClient.Send(mailMessage);
}
}
</script>
Visual studio gives an error over and over again with my script, even from the void part...
Set OnUpdated-handler
<asp:SqlDataSource
...
UpdateCommand="..."
OnUpdated="OnDSUpdatedHandler">
</asp:SqlDataSource>
and use it to send email
<script runat="server">
private void OnDSUpdatedHandler(Object source, SqlDataSourceStatusEventArgs e) {
if (e.AffectedRows > 0) {
MailMessage mailMessage = new MailMessage();
...
SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
smtpClient.Send(mailMessage);
}
}
</script>

Deleting from SQL detailsView

Hey I am just learning to how to use asp.net and I am trying to use the grid view to display information and details view edit/delete the information from the database. Right now I can edit and update the information in the database but I also want it to delete and something is not working for me. I set up an exception and when I select the item and press delete it pops up my exception that another user has deleted the item when it hasn't. Im not really sure what i have done wrong, to me it looks like it should work.
Here is the code for the delete command and parameters from the details view
DeleteCommand="DELETE FROM [Customers]
WHERE [CustomerID] = #original_CustomerID
AND [Name] = #original_Name
AND [Address] = #original_Address
AND [City] = #original_City
AND [State] = #original_State
AND [ZipCode] = #original_ZipCode
AND ([Phone] = #original_Phone
OR [Phone] IS NULL AND #original_Phone IS NULL)
AND ([Email] = #original_Email
OR [Email] IS NULL AND #original_Email IS NULL)"
<DeleteParameters>
<asp:Parameter Name="original_CustomerID" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_City" Type="String" />
<asp:Parameter Name="original_State" Type="String" />
<asp:Parameter Name="original_ZipCode" Type="String" />
<asp:Parameter Name="original_Phone" Type="String" />
<asp:Parameter Name="original_Email" Type="String" />
</DeleteParameters>
and here is the delete functions in the cs file
protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = "A database error has occurred.<br /><br />" +
"Message: " + e.Exception.Message;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == 0)
lblError.Text = "Another user may have deleted that customer."
+ "<br />Please try again.";
else
GridView1.DataBind();
}
protected void DetailsView1_ItemDeleting(
object sender, DetailsViewDeleteEventArgs e)
{
e.Values["Name"]
= e.Values["Name"].ToString().Substring(1);
}
Why do you have such a huge query for a delete statement?Have a look how it's done in the MSDN DetailsView example
The CustomerID in your SQL table should be unique so your delete statement should be as simple as:
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = #original_CustomerID"
And only one delete parameter:
<DeleteParameters>
<asp:Parameter Name="original_CustomerID" Type="Int32" />
</DeleteParameters>
One more thing which is quite important is you need to set DataKeyNames="CustomerID" on the DetailsView so that DetailsView1_ItemDeleted knows which row to delete. Hope this helps!

setting the default value for insert parameters in sql datasource asp.net

i have a datasource, which has an insertparameters, one of which whose type is set to Boolean, but while adding it from code behind insertparameters.defaultvalue always return string.
code behind code:
if (e.CommandName == "InsertNew")
{
TextBox Title = GridView1.FooterRow.FindControl("txtAddTitle") as TextBox;
TextBox Quote = GridView1.FooterRow.FindControl("txtAddQuote") as TextBox;
TextBox QuoteLink = GridView1.FooterRow.FindControl("txtAddQuoteLink") as TextBox;
CheckBox Published = GridView1.FooterRow.FindControl("chkAddBox") as CheckBox;
TextBox PublishedDate = GridView1.FooterRow.FindControl("txtAddPublishedDate") as TextBox;
SqlDataSource1.InsertParameters["Title"].DefaultValue = Title.Text;
SqlDataSource1.InsertParameters["Quote"].DefaultValue = Quote.Text;
SqlDataSource1.InsertParameters["QuoteLink"].DefaultValue = QuoteLink.Text;
SqlDataSource1.InsertParameters["Published"].DefaultValue = Convert.ToString(Published.Checked);
SqlDataSource1.InsertParameters["PublishedDate"].DefaultValue = PublishedDate.Text;
SqlDataSource1.Insert();
}
and aspx code for sql datasource is:
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Quote" Type="String" />
<asp:Parameter Name="QuoteLink" Type="String" />
<asp:Parameter Name="Published" Type="Boolean" />
<asp:Parameter Name="PublishedDate" Type="DateTime" />
</InsertParameters>
Please reply how should i set the datatype as boolean from code behind.
1: Not sure why you are setting DefaultValue instead of the value
Or
2: Can you try this:
SqlDataSource1.InsertParameters["Published"].DefaultValue = Published.Checked==true?"true":"false";

ObjectDataSource Update/Delete parameters from GridView

<asp:ObjectDataSource ID="MMBRSODS" runat="server"
OldValuesParameterFormatString="original_{0}"
TypeName="Flow_WEB_Nemerle.SQLModule"
SelectMethod="GetMembers"
UpdateMethod="UpdateMember"
DeleteMethod="DeleteMember">
<UpdateParameters>
<asp:Parameter Name="UserName" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="UserName" Type="String" />
</DeleteParameters>
</asp:ObjectDataSource>
Select method returns array of strings . But How I need to appear selected node for edit delete methods get this string (userName) ?
for methods like :
[DataObjectMethod(DataObjectMethodType.Delete, true)]
static public DeleteMember(...
What are you binding the original list against? Let's say you are binding a list; you can always do the following to do that type of operation:
MMBRSODS.UpdateParameters["UserName"].DefaultValue = list.SelectedValue;
MMBRSODS.Update();
So supplying the default value allows you to still use the update, the param relies on this default and supplies it to the backend... what control are you using to show the data?
HTH.

ASP.NET [Object data source]

I send id of my class object from one page to second this way :
NavigateUrl='<%# "ItemDetail.aspx?itemId=" + (string)Eval("Id") %>'
Then I get the object with ObjectDataSource and function this way :
<asp:ObjectDataSource ID="ObjectDataSourceItem" runat="server" SelectMethod="GetItem"
TypeName="Catalog">
<SelectParameters>
<asp:QueryStringParameter Name="itemId" QueryStringField="itemId" Type="string" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
So how to use this item in my new page code :
this.ObjectDataSourceItem.?????
You need to subscribe to the ObjectDataSource's Selected event, access the ObjectDataSourceStatusEventArgs ReturnValue property (e.ReturnValue), and cast it to the appropriate type. Something like:
protected void ObjectDataSourceItem_Selected(object source, ObjectDataSourceStatusEventArgs e)
{
var myDataSet = (DataSet)e.ReturnValue;
}

Resources