-1

I have made a web service to insert product details

the web method is as follows

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    private string strConn = "";
    public Service()
    {

        strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

    }

    [WebMethod]
    public void insert_prod(string ppid, string ppname, object sender, long ppnormal_price, string ppstorage, long ppqty, long ppreorder_pts, long ppreorder_qty, string ppdesc,long pppcp)
    {
        SqlConnection cnn = new SqlConnection(strConn);
        cnn.Open();
        SqlCommand cmd = new SqlCommand();
        

        cmd.Connection = cnn;
        cmd.CommandText = "insert into products(product_id,prod_name,prod_category,prod_normal_price,prod_storage_area,prod_qty,prod_reorder_pts,prod_reorder_qty,prod_description,prod_current_price)values(@pid,@pname,@pcat,@pnp,@psa,@pqty,@prep,@preq,@pdesc,@pcp)";

        DropDownList ddl = (DropDownList)sender;    



        SqlParameter pid = new SqlParameter("@pid", ppid);
        SqlParameter pname = new SqlParameter("@pname", ppname);
        
        SqlParameter pnormal_price = new SqlParameter("@pnp", ppnormal_price);
        SqlParameter pstorage_area = new SqlParameter("@psa", ppstorage);
        SqlParameter pqty = new SqlParameter("@pqty", ppqty);
        SqlParameter preorder_pts = new SqlParameter("@prep", ppreorder_pts);
        SqlParameter preorder_qty= new SqlParameter("@preq", ppreorder_qty);
        SqlParameter pdesc = new SqlParameter("@pdesc", ppdesc);
        SqlParameter pcp = new SqlParameter("@pcp", pppcp);

        
        cmd.Parameters.Add(pid);
        cmd.Parameters.Add(pname);
        cmd.Parameters.Add("@pcat",System.Data.SqlDbType.NVarChar);
        cmd.Parameters["@pcat"].Value = ddl.SelectedItem.Value.ToString();
        cmd.Parameters.Add(pnormal_price);
        cmd.Parameters.Add(pstorage_area);
        cmd.Parameters.Add(pqty);
        cmd.Parameters.Add(preorder_pts);
        cmd.Parameters.Add(preorder_qty);
        cmd.Parameters.Add(pdesc);
        cmd.Parameters.Add(pcp);

        cmd.ExecuteNonQuery();
        cnn.Close();

    }
    
    
}

and the web form is as follows

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <br />
        <center><asp:Label ID="Label10" runat="server" Text="Enter Product Details"></asp:Label></center>
        <br />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Product Id"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" style="margin-left: 119px"></asp:TextBox>
    
    </div>
    <p>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 146px"></asp:TextBox>
    </p>
    <p>
        <asp:Label ID="Label3" runat="server" Text="Product Category"></asp:Label>
        <asp:DropDownList ID="DropDownList2" runat="server" style="margin-left: 75px" 
            Width="131px" DataSourceID="SqlDataSource1" DataTextField="product_category" 
            DataValueField="prod_category" AutoPostBack="True">
            <asp:ListItem>casting</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:fastinventoryConnectionString %>" 
            SelectCommand="SELECT [product_category] FROM [product_category]">
        </asp:SqlDataSource>
    </p>
    <asp:Label ID="Label4" runat="server" Text="Normal Price"></asp:Label>
    <asp:TextBox ID="TextBox4" runat="server"  style="margin-left: 102px"></asp:TextBox>
    <p>
        <asp:Label ID="Label5" runat="server" Text="Storage Area"></asp:Label>
        <asp:DropDownList ID="DropDownList1" runat="server" style="margin-left: 104px" 
            Width="124px" DataSourceID="SqlDataSource2" 
            DataTextField="product_location" DataValueField="product_location" >
            <asp:ListItem>main warehouse</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:fastinventoryConnectionString %>" 
            SelectCommand="SELECT [product_location] FROM [locations]">
        </asp:SqlDataSource>
    </p>
    <p>
        <asp:Label ID="Label6" runat="server" Text="Quantity"></asp:Label>
        <asp:TextBox ID="TextBox6" runat="server" style="margin-left: 133px"></asp:TextBox>
    </p>
    <asp:Label ID="Label7" runat="server" Text="Reorder Points"></asp:Label>
    <asp:TextBox ID="TextBox7" runat="server" style="margin-left: 94px"></asp:TextBox>
    <p>
        <asp:Label ID="Label8" runat="server" Text="Reorder Quantity"></asp:Label>
        <asp:TextBox ID="TextBox8" runat="server" style="margin-left: 80px"></asp:TextBox>
    </p>
    <asp:Label ID="Label9" runat="server" Text="Description"></asp:Label>
    <asp:TextBox ID="TextBox9" runat="server" style="margin-left: 115px"></asp:TextBox>
    <p>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            style="margin-left: 126px" Text="Submit" Width="97px" />
            
         <script type="text/javascript" language='javascript'>function AlertHello() { 
             alert('Product Added Successfully'); }</script>
             
        <asp:Button ID="Button2" runat="server" style="margin-left: 41px" Text="Reset" 
            Width="100px" />
    </p>
    <asp:Label ID="Label11" runat="server"></asp:Label>
    </form>
</body>
</html>

the error i get is
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'prod_category'.


mu submit button logic is

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

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        new_prod.Service a = new new_prod.Service();

        

        a.insert_prod(this.TextBox1.Text, this.TextBox2.Text, this.DropDownList2.SelectedItem.Value, long.Parse(this.TextBox4.Text), this.DropDownList1.SelectedItem.Value.ToString(), long.Parse(this.TextBox6.Text), long.Parse(this.TextBox7.Text), long.Parse(this.TextBox8.Text), this.TextBox9.Text, long.Parse(this.TextBox4.Text));


        Response.Write("<script language='javascript'>alert('Product Added Successfully');</script>");
    }
    
}

i think the problem is caused because the datasource for dropdownlist is from different table named "product_category" but i am inserting value into other table "product" which contains product_category as one attribute

Votes + Comments
and you posted that in community feedback why?
2
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by jbisono
0
<asp:DropDownList ID="DropDownList2" runat="server" style="margin-left: 75px" 
   Width="131px" DataSourceID="SqlDataSource1"    DataTextField="product_category" 
   DataValueField="product_category" AutoPostBack="True">
   <asp:ListItem>casting</asp:ListItem>
</asp:DropDownList>

Like the error says you, there is no prod_category and your datasource, so you have to change it for the real name which is product_category base on your select statement.

Edited by jbisono: Fix code

0
<asp:DropDownList ID="DropDownList2" runat="server" style="margin-left: 75px" 
   Width="131px" DataSourceID="SqlDataSource1"    DataTextField="product_category" 
   DataValueField="product_category" AutoPostBack="True">
   <asp:ListItem>casting</asp:ListItem>
</asp:DropDownList>

Like the error says you, there is no prod_category and your datasource, so you have to change it for the real name which is product_category base on your select statement.

thnx finally atleast the form is loading but now i am getting following error

Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.DropDownList'.

0

In your webmethod you have this line
DropDownList ddl = (DropDownList)sender;
"sender" in this case is a button and you are trying to convert a button to dropdownlist, which that won't happen.

Change the "object sender" parameter in your function to "string pcategory" or whatever you want it to call it, and use that, you are already sending a string so there is no need to do what you are trying to do.

0

In your webmethod you have this line
DropDownList ddl = (DropDownList)sender;
"sender" in this case is a button and you are trying to convert a button to dropdownlist, which that won't happen.

Change the "object sender" parameter in your function to "string pcategory" or whatever you want it to call it, and use that, you are already sending a string so there is no need to do what you are trying to do.

thank you very much atlast its solves but i know its silly my alert is not working but still the thread is answered

0

You can do this to display an alert.
ScriptManager.RegisterStartupScript(sender, sender.GetType(), "alert", "alert('Product Added Successfully');", true);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.