0
here is the front end

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo1.aspx.cs" Inherits="WebApplication1.l" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
   
            <asp:Label ID="Label1" runat="server" Text="Party Name"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
            <asp:Label ID="Label2" runat="server" Text="Invoice No. :"></asp:Label>

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
            <asp:Label ID="Label3" runat="server" Text="Invoice Date :"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
                 <Columns>  
                     <asp:BoundField Datafield="SrNo" HeaderText="SrNo"/>
                     <asp:TemplateField HeaderText="Item Name">
                         <ItemTemplate>
                             <asp:DropDownList ID="Name" runat="server" DataTextField="Name">
                             </asp:DropDownList>
                         </ItemTemplate>
                         </asp:TemplateField>
                     <asp:TemplateField HeaderText="Name">
                         <ItemTemplate>
                          <asp:textbox ID="UOM" runat="server" DataTextField="Name" />
                         </ItemTemplate>

                         </asp:TemplateField>
<%--                     <asp:BoundField Datafield="uom" HeaderText="uom"/>--%>
                     <asp:BoundField DataField="qty" HeaderText="qty"/>
                     <asp:BoundField Datafield="Rate" HeaderText="Rate"/>
                     <asp:BoundField DataField="value" HeaderText="Value"/>




                        

                     
                     
                 </Columns>

            </asp:GridView>

             





        </div>
    </form>
</body>
</html>

here is the code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
    public partial class l : System.Web.UI.Page
    {
        string str = ConfigurationManager.ConnectionStrings["contstring"].ConnectionString;
        //DataTable rstcity;
        DataTable dt1;
        DataTable dt2;
        DataTable dt3;
        //TextBox uom1;
        //DataTable rstcity1;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                //if(!IsPostBack)
                //{
                //    loadgrid();

                //}

                loadgrid();

                //try
                //{

                //    SqlConnection con = new SqlConnection(str);
                //    string com = "select Party,Name from PartyMaster";
                //    SqlDataAdapter adpt = new SqlDataAdapter(com, con);
                //    DataTable dt = new DataTable();
                //    adpt.Fill(dt);
                //    DropDownList1.DataSource = dt;

                //    DropDownList1.DataBind();
                //    DropDownList1.DataTextField = "Name";
                //    DropDownList1.DataValueField = "Party";
                //    DropDownList1.DataSource = dt;
                //    DropDownList1.DataBind();


                //    con.Open();

                //    SqlCommand cmd = new SqlCommand("select SrNo,qty,Rate from DOCDET ", con);
                //    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                //    DataTable dt1 = new DataTable();
                //    sda.Fill(dt1);
                //    con.Close();
                //    GridView1.DataSource = dt1;
                //    GridView1.DataBind();


                //}

                //catch(Exception ex)
                //{

                //}
            }
            catch (Exception ex)

            {
                //Response.Write(ex.Message);
                Console.WriteLine(ex.Message);
            }


         }




        //public void getData()
        //{
        //    //GridViewRowEventArgs e = new GridViewRowEventArgs();

        //    rstcity = new DataTable();
        //    SqlConnection conn = new SqlConnection(str);
        //    conn.Open();
        //    string strsql1 = "select unit_of_measurement.Name  from unit_of_measurement inner join ItemMaster on unit_of_measurement.UOM=ItemMaster.UID";

        //    SqlCommand cmd = new SqlCommand(strsql1, conn);

        //    SqlDataAdapter sda = new SqlDataAdapter(cmd);
        //    sda.Fill(rstcity);
        //    if (rstcity.Rows.Count > 0)
        //    {
        //        //Text = rstcity.Rows[0]["Name"].ToString();
        //         TextBox UOM1 = (TextBox)e.Row.FindControl("UOM");
          //        UOM1.Text = rstcity.Rows[0]["UOM"].ToString();


        //    }
       
        //}

        public void loadgrid()
         {

            strSQL = "Select Name from ItemMaster";
           dt1 = MyRst(strSQL);
          strSQL = "select SrNo,qty,Rate,Value from DOCDET";
            dt2 = MyRst(strSQL);
           
            dt1.Merge(dt2);
            dt1.AcceptChanges();
            GridView1.DataSource = dt1;
            GridView1.DataBind();

        }
              
        public DataTable MyRst(string strSQL)
        {
            var rstData = new DataTable();
            using (var conn = new SqlConnection(str))
            {
                using (var cmdSQL = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    rstData.Load(cmdSQL.ExecuteReader());
                    //rstData.Load();
                    rstData.TableName = strSQL;
                    conn.Close();

                   // GridView1.DataSource = rstData;
                    //GridView1.DataBind();
                }
            }
           
            //rstData.Load(cmd.ExecuteReader);
           
                
            return rstData;
        }

        public DataTable myRst1(string strsql)
        {
           var rstdata1 = new DataTable();
            using (var conn1 = new SqlConnection(str))
            {


               using (var cmdsql1 = new SqlCommand(strsql, conn1))
                {
                  conn1.Open();
                    rstdata1.Load(cmdsql1.ExecuteReader());

                  rstdata1.TableName=strsql;
                    conn1.Close();

                    

                }

            }
            return rstdata1;
        }
        

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                DropDownList name = (DropDownList)e.Row.FindControl("Name");
                name.DataSource = dt1;
                name.DataBind();
                name.Items.Insert(0,new ListItem(" Select name"));





            }


            SqlConnection con = new SqlConnection(str);
            con.Open();
            string strsql = "select unit_of_measurement.Name from unit_of_measurement join ItemMaster on unit_of_measurement.UOM=ItemMaster.UID";
            dt3 = myRst1(strsql);
            SqlCommand cmd = new SqlCommand(strsql, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt3);
            TextBox UOM = (TextBox)e.Row.FindControl("Name");

            if (dt3.Rows.Count > 0)
            {
                UOM.Text = dt3.Rows[0]["Name"].ToString();
            }
            con.Close();
           

        }






    }


    

}
   

Here is my database details

enter image description here

enter image description here

Iam debugging the above code but iam getting run time exception "object reference not set to an instance of an object" iam not understand why?? Please provide me solution on what to do on that exception. I have also send images of database

0 Answers0