Click here to Skip to main content
14,697,269 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:

I am trying to make a order by gridview.

I am using dropdown for item name selection and the next column is label display price of that item.
The next column is textbox quantity inserted by user and last column is label amount display calculated amount on the basis of price and quantity.

Find following my code to provide me a solution.

<telerik:RadGrid ID="radGrid1"  runat="server" OnInsertCommand="radGrid1_InsertCommand" AutoGenerateColumns="False" GridLines="None"
            AllowMultiRowEdit="True" AllowAutomaticInserts="true" OnItemDataBound="radGrid1_ItemDataBound"  önNeedDataSource="radGrid1_NeedDataSource">
            <mastertableview autogeneratecolumns="false" editmode="InPlace" showfooter="false" commanditemdisplay="Top" önprerender="radGrid1_PreRender">
                    <telerik:gridtemplatecolumn headertext="Product Name" uniquename="Product_Name" xmlns:telerik="#unknown">
                            <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" DataTextField="product_Name"
                                DataValueField="product_Name" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
                    <telerik:gridtemplatecolumn headertext="Product_rate" xmlns:telerik="#unknown">
                            <asp:Label ID="lblrate" runat="server">
                    <telerik:gridtemplatecolumn headertext="Product Quantity" xmlns:telerik="#unknown">
                            <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("Product_Quantity")%>' />
                    <telerik:gridtemplatecolumn headertext="Product Amount" xmlns:telerik="#unknown">
                            <asp:Label ID="lblAmount" runat="server" />

javascript code is...

<script type="text/javascript" language="javascript">
        function show(labelrate, labelamount, txtqty) {
            var txt_Quantity = $find(txtqty);
            var lbl_Rate = $find(labelrate)
            var lbl_Amount = $find(labelamount);
            //lbl_Amount.innerHTML = parseInt(txt_Quantity.get_value()) * parseInt(lbl_Rate.get_value());
            var total = txt_Quantity.Getvalue() * lbl_Rate.Getvalue();

protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            if (e.Item is GridDataItem || e.Item.IsInEditMode)
                GridDataItem item = (GridDataItem)e.Item;
                DropDownList list = (DropDownList)item.FindControl("ddl1");
                list.Items.Insert(0, new ListItem("select", "-1"));
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("select product_name from product_detail", con);
                DataSet ds = new DataSet();
                list.DataSource = ds;

                //For calculate item Amount....
                Label lbl_Rate = (Label)item.FindControl("lblRate");

                TextBox txt_Quantity = (TextBox)item.FindControl("txtQuantity");
                Label lbl_Amount = (Label)item.FindControl("lblAmount");

/*Object reference not set to an instance of an object.*/

                txt_Quantity.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
                lbl_Rate.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
                lbl_Amount.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");


 protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
            DropDownList list = (DropDownList)sender;
            GridDataItem item = (GridDataItem)list.NamingContainer;

            //display selected value of dropdown list.

            //string str = list.SelectedValue;
            //(item.FindControl("lblRate") as Label).Text = str;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select product_rate from product_detail where product_name='" + list.SelectedItem.Text + "'", con);
            SqlDataReader reader; ;
            reader = cmd.ExecuteReader();
            if (reader.Read())
                (item.FindControl("lblRate") as Label).Text = reader["product_rate"].ToString();

Updated 27-Oct-10 22:54pm
Sunasara Imdadhusen 28-Oct-10 3:19am
What's your problem?
Sandeep Mewara 28-Oct-10 3:23am
Whats the error/issue?
call to .net 28-Oct-10 3:45am
when i debug throw a message
//Object reference not set to an instance of an object.
may i know what is the reason??
i have mark a comment.
Dalek Dave 28-Oct-10 3:54am
Edited for Grammar, Syntax and Readability.
Sunasara Imdadhusen 28-Oct-10 4:21am
You are getting error during radGrid1_ItemDataBound event?
call to .net 28-Oct-10 5:33am
yes u r right
Sandeep Mewara 28-Oct-10 6:43am
There are lots of potential place for that error. Can specify the line number?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900