Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

this is my control.ascx page

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="control.ascx.cs" Inherits="control" %>
<table>
<tr>
<td align="right">Name:</td>
<td align="right"><asp:TextBox runat="server" ID="txtName"></asp:TextBox></td>
</tr>
<tr>
<td align="right">LastName:</td>
<td align="right"><asp:TextBox runat="server" ID="txtLastName"></asp:TextBox></td>
</tr>
<tr>
<td align="right">PhoneNo:</td>
<td align="right"><asp:TextBox runat="server" ID="txtPhone"></asp:TextBox></td>
</tr>
</table>



this is my control.ascx.cs page

C#
using System;
using System.Collections;
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 control : System.Web.UI.UserControl
{
    public string name                 
    {
        get
        {
            return txtName.Text;     ****************-here am getting error"""Object reference not set to an instance of an object"**************
        }
        set
        {
            txtName.Text = value;
        }
    }
    public string lastname
    {
        get
        {
            return txtName.Text;
        }
        set
        {
            txtName.Text = value;
        }
    }
    public string phoneno
    {
        get
        {
            return txtPhone.Text;
        }
        set
        {
            txtPhone.Text = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }


}


this is my default.aspx page

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



<%@ Register src="control.ascx" tagname="control" tagprefix="uc1" %>



<!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>


        <uc1:control ID="control1" runat="server" />


    </div>
  <br />
    <asp:Button runat="server" ID="btnInsert" Text="insert"
        onclick="btnInsert_Click" />
    </form>
</body>
</html>



this is my default.aspx.cs page


C#
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;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection("user id=sa;password=123;database=srikanth;data source=sri");
    SqlCommand cmd = new SqlCommand();
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        control c = new control();
        
      
        
        con.Open();
        cmd = new SqlCommand("insert into emp(name,lastname,phone) values('" +c.name  + "','" + c.lastname + "','" + c.phoneno + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    
}

Posted
Updated 5-Dec-12 3:18am
v2

The problem is in your Default.aspx.cs

In the button click event, you must use the control ID ie. control1.name which you have used in your Default.aspx page.

the code should be as below

C#
protected void btnInsert_Click(object sender, EventArgs e)
          {
          con.Open();
          cmd = new SqlCommand("insert into emp(name,lastname,phone) values('" + control1.name + "','" + control1.lastname + "','" + control1.phoneno + "'", con);
          cmd.ExecuteNonQuery();
          con.Close();
          }
 
Share this answer
 
make this kind of change in your control.ascx.cs
C#
string name = txtName.Text;
public string _name
   {
       get
       {
           return name;
       }
       set
       {
           name = value;
       }
   }


Hope this will work (blindly assumption)
 
Share this answer
 
Comments
Teenustar 5-Dec-12 11:03am    
This solution won't solve the problem.
Are you getting any error. You have not mentioned what you exactly want.
 
Share this answer
 
Comments
Member 9560415 5-Dec-12 8:38am    
ya am getting error..... i mentioned error message in default.aspx.cs page
Member 9560415 5-Dec-12 8:40am    
sorry control.ascx.cs page

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