locked
Object reference not set to an instance of an object. RRS feed

  • Question

  • hi,

    In gridview  if i click on id column  want to populate that data into textboxes in another webform .

      

    in webform2.aspx

    private int userid = 0; protected void Page_Load(object sender, EventArgs e) { userid = Convert.ToInt32(Request.QueryString["id"].ToString()); BindControlvalues(); } private void BindControlvalues() { string Connection = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; SqlConnection cn = new SqlConnection(Connection); cn.Open(); SqlCommand cmd = new SqlCommand("select * from EmpTimesheet where EmpId=" + userid, cn); SqlDataAdapter da = new SqlDataAdapter(cmd); cn.Close(); DataSet ds = new DataSet(); da.Fill(ds); txt_EmpName.Text = ds.Tables[0].Rows[0][1].ToString(); txt_StartDate.Text = ds.Tables[0].Rows[0][2].ToString(); txt_EndDate.Text = ds.Tables[0].Rows[0][3].ToString(); // txtemail.Text = ds.Tables[0].Rows[0][4].ToString(); }

     

    webform1.aspx

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Height="267px" Width="1170px" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" onrowediting="GridView1_RowEditing" > <RowStyle BackColor="#EFF3FB" /> <Columns> <asp:CommandField ShowCancelButton="False" ShowEditButton="True" /> <asp:HyperLinkField DataTextField="EmpID" HeaderText="TimesheetID" DataNavigateUrlFields="EmpID" DataNavigateUrlFormatString="Timesheet3.aspx?mode=view&id{0}" SortExpression="EmpID"/> <asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" /> <asp:BoundField DataField="StartDate" HeaderText="StartDate" /> <asp:BoundField DataField="EndDate" HeaderText="EndDate" /> <asp:BoundField DataField="day" HeaderText="Day" /> <asp:BoundField DataField="date" HeaderText="Date" /> <asp:BoundField DataField="Service" HeaderText="Service" /> <asp:BoundField DataField="Venue" HeaderText="Venue" /> <asp:BoundField DataField="StartTime" HeaderText="StartTime" /> <asp:BoundField DataField="EndTime" HeaderText="EndTime" /> <asp:BoundField DataField="DailyTotal" HeaderText="DailyTotal" /> <asp:CommandField /> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView>

    in webform1 when pageloads it  gets  data from database  into gridview
    id empname startdate           enddate            day            date          services          venue      ,br,   lunch       dinner  total

    1   ram         01/05/2014       01/05/2014       sun       01/05/2014      ....

    1   ram         01/05/2014       01/05/2014       mon       01/06/2014      ....

    1   ram         01/05/2014       01/05/2014       tue       01/07/2014      ....

    1   ram         01/05/2014       01/05/2014       wed      01/08/2014      ....

    1   ram         01/05/2014       01/05/2014       thurs      01/09/2014      ....

    1   ram         01/05/2014       01/05/2014       fri         01/10/2014      ....

    1   ram         01/05/2014       01/05/2014       sat      01/11/2014      ....

    If i click on id =1 want to populate all data into teextboxes in webform2.aspx



    • Moved by Caillen Tuesday, January 28, 2014 3:16 AM
    Monday, January 27, 2014 9:20 PM

Answers

  • Use the cell click event like the code below.  You can get an array from the datatable and pass the array to your webform.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        
        public partial class Form1 : Form
        {
     
            DataSet ds;
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                BindControlvalues();
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            private void BindControlvalues()
            {
                //I simulated the SQL query
                //id empname startdate           enddate            day            date          services          venue      ,br,   lunch       dinner  total
                List<List<object>> input = new List<List<object>> {
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"sun", DateTime.Parse("01/05/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"mon", DateTime.Parse("01/06/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"tue", DateTime.Parse("01/07/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"wed", DateTime.Parse("01/08/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"thurs", DateTime.Parse("01/09/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"fri", DateTime.Parse("01/10/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"sat", DateTime.Parse("01/11/2014")},
                };
    
                ds = new DataSet();
                DataTable table = new DataTable();
                ds.Tables.Add(table);
                table.Columns.Add("id", typeof(int));
                table.Columns.Add("empname", typeof(string));
                table.Columns.Add("startdate", typeof(DateTime));
                table.Columns.Add("enddate", typeof(DateTime));
                table.Columns.Add("day", typeof(string));
                table.Columns.Add("date", typeof(DateTime));
    
                foreach (List<object> inputRow in input)
                {
                    DataRow newRow = table.Rows.Add();
                    newRow.ItemArray = inputRow.ToArray();
                }
                dataGridView1.DataSource = table;
            }
    
           
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    object[] rowData = ds.Tables[0].Rows[e.RowIndex].ItemArray;
                }
            }
    
        }
    }
    


    jdweng

    • Proposed as answer by Just Karl Tuesday, January 28, 2014 4:17 PM
    • Marked as answer by emaak Tuesday, January 28, 2014 10:26 PM
    Tuesday, January 28, 2014 4:46 AM

All replies

  • Hi hemaak,

    Please post ASP.NET questions in ASP.NET forums where you could get better responses.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Just Karl Tuesday, January 28, 2014 4:17 PM
    Tuesday, January 28, 2014 3:15 AM
  • Use the cell click event like the code below.  You can get an array from the datatable and pass the array to your webform.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        
        public partial class Form1 : Form
        {
     
            DataSet ds;
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                BindControlvalues();
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            private void BindControlvalues()
            {
                //I simulated the SQL query
                //id empname startdate           enddate            day            date          services          venue      ,br,   lunch       dinner  total
                List<List<object>> input = new List<List<object>> {
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"sun", DateTime.Parse("01/05/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"mon", DateTime.Parse("01/06/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"tue", DateTime.Parse("01/07/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"wed", DateTime.Parse("01/08/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"thurs", DateTime.Parse("01/09/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"fri", DateTime.Parse("01/10/2014")},
                    new List<object> {"1","ram", DateTime.Parse("01/05/2014"),DateTime.Parse("01/05/2014"),"sat", DateTime.Parse("01/11/2014")},
                };
    
                ds = new DataSet();
                DataTable table = new DataTable();
                ds.Tables.Add(table);
                table.Columns.Add("id", typeof(int));
                table.Columns.Add("empname", typeof(string));
                table.Columns.Add("startdate", typeof(DateTime));
                table.Columns.Add("enddate", typeof(DateTime));
                table.Columns.Add("day", typeof(string));
                table.Columns.Add("date", typeof(DateTime));
    
                foreach (List<object> inputRow in input)
                {
                    DataRow newRow = table.Rows.Add();
                    newRow.ItemArray = inputRow.ToArray();
                }
                dataGridView1.DataSource = table;
            }
    
           
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    object[] rowData = ds.Tables[0].Rows[e.RowIndex].ItemArray;
                }
            }
    
        }
    }
    


    jdweng

    • Proposed as answer by Just Karl Tuesday, January 28, 2014 4:17 PM
    • Marked as answer by emaak Tuesday, January 28, 2014 10:26 PM
    Tuesday, January 28, 2014 4:46 AM