使用用户控件将数据插入数据库 [英] Inserting data into database using user control

查看:64
本文介绍了使用用户控件将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的control.ascx页面



 <%@    控制   语言  =  C#    AutoEventWireup   =  true    CodeFile   =  control.ascx.cs   继承  =  control   %>  
< table >
< tr >
< td align = right > 名称:< / td >
< td align = right > < asp:TextBox runat = server ID = txtName > < / asp:TextBox > < / td >
< / tr >
< tr >
< td align = right > 姓氏:< / td >
< td align = right > < asp:TextBox runat = server ID = txtLastName > < / as p:TextBox > < / td >
< / tr >
< tr >
< td align = right > PhoneNo:< / td >
< td align = right > < asp:TextBox runat = server ID = txtPhone > < / asp:TextBox > < / td < span class =code-keyword>>

< / tr >
< / table >







this是我的control.ascx.cs页面



 使用系统; 
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;

public partial class control:System.Web.UI.UserControl
{
public string name
{
get
{
return txtName.Text; **************** - 这里收到错误 对象引用未设置为对象的实例 ********* *****
}
set
{
txtName.Text = ;
}
}
public string lastname
{
get
{
return txtName.Text;
}
set
{
txtName.Text = value ;
}
}
public string phoneno
{
get
{
return txtPhone.Text;
}
set
{
txtPhone.Text = value ;
}
}
受保护 void Page_Load( object sender,EventArgs e)
{

}


}





这是我的default.aspx页面



 <%@     Page    语言  =  C#     AutoEventWireup   =  true      CodeFile   =  Default.aspx.cs   继承  =  _默认   %>  



<%@ 注册 < span class =code-attribute> 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 > 无标题页< / title >
< / head >
< body >
< 表格 id = form1 runat = 服务器 >
< div >


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


< / div >
< br / >
< asp:按钮 runat = server < span class =code-attribute> ID = btnInsert 文本 = insert

onclick = btnInsert_Click / >
< / form >
< / body >
< / html >







这是我的default.aspx.cs页面





 使用系统; 
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 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();

受保护 void Page_Load(对象发​​件人,EventArgs e)
{

}
protected void btnInsert_Click( object sender,EventArgs e)
{
control c = new control();



con.Open();
cmd = new SqlCommand( insert into emp(姓名,姓氏,电话)值(' + c.name + ',' + c.lastname + ',' + c.phoneno + ,con);
cmd.ExecuteNonQuery();
con.Close( );
}

}



解决方案

< blockquote>问题出在你的Default.aspx.cs中



在按钮点击事件中,你必须使用控件ID,即。 control1.name 你在Default.aspx页面中使用过的。



代码应如下所示



  protected   void  btnInsert_Click( object  sender,EventArgs e)
{
con.Open();
cmd = new SqlCommand( insert into emp(姓名,姓氏,电话)值(' + control1.name + ',' + control1.lastname + ',' + control1.phoneno + ,con);
cmd.ExecuteNonQuery();
con.Close( );
}


control.ascx.cs
$ b中进行此类更改$ b

  string  name = txtName.Text; 
public string _name
{
get
{
< span class =code-keyword> return name;
}
设置
{
name = value ;
}
}





希望这会奏效(盲目假设)


< blockquote>你收到任何错误吗?你没有提到你想要的东西。



this is my control.ascx page

<%@ 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

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

<%@ 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


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();
    }
    
}


解决方案

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

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();
          }


make this kind of change in your control.ascx.cs

string name = txtName.Text;
public string _name
   {
       get
       {
           return name;
       }
       set
       {
           name = value;
       }
   }



Hope this will work (blindly assumption)


Are you getting any error. You have not mentioned what you exactly want.


这篇关于使用用户控件将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆