我试图更新,但它没有解雇..什么是它的错误请帮助.. [英] i trying to update but its not firing..what is the error on it please help..

查看:46
本文介绍了我试图更新,但它没有解雇..什么是它的错误请帮助..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Button2_Click(对象发​​件人,EventArgs e)
{
尝试
{
if (TextBox1.Text!=
{
conn.Open();

SqlCommand cmd = new SqlCommand( demospedit4,conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter id = cmd.Parameters.Add( @ id,SqlDbType.BigInt) ;
id.Direction = ParameterDirection.Output;
SqlParameter name = cmd.Parameters.Add( @ name,SqlDbType.NVarChar) ;
name.Value = TextBox1.Text;

SqlParameter phone = cmd.Parameters.Add( @ phone, SqlDbType.BigInt);
phone.Value = TextBox2.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (例外情况)
{
Response.Write(ex.ToString( ));
}
最后
{
conn.Close();
gBind();
clearall();
}



  USE  [demosp] 
GO
/ * *****对象:StoredProcedure [dbo]。[demospedit4]脚本日期:05/29/2015 12:09:25 ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo]。[demospedit4]( @ id bigint 输出 @ name nvarchar 20 ), @ phone varchar 20 ))
as
begin
更新 demosptable 设置 phone = @ phone,name = @ name 来自 dbo.demosptable 其中 id = @ id
return @ id
end

解决方案

< blockquote>你没有给你的@id参数一个值,所以它没有任何更新。



 SqlParameter id = cmd.Parameters 。添加  @ id, SqlDbType。 BigInt ); 
id.Direction = ParameterDirection。输出;
//id.Value = ????

SqlParameter name = cmd.Parameters。添加 @ name,SqlDbType。 NVarChar );
name.Value = TextBox1。文字;






你需要传递你想要更新的记录的id值。

 id name phone 
1 madhuri 456
2 manoj 440



请查看上表作为示例: -

1.如果ypu想要更新'madhuri'的电话号码,那么你需要传递id = 1然后只有你的程序会更新记录。



2。示例: -

 SqlParameter id = cmd.Parameters.Add(  @ id,SqlDbType.BigInt); 
id.Direction = ParameterDirection.InputOutput;
id.Value = TextBox3.Text;



在文本框3中,您可以输入值1来更新madhuri的电话号码。



请查看以下代码供您参考: -

Default.aspx =

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

< html xmlns = http:// www。 w3.org/1999/xhtml\">
< head runat = 服务器 >
< title > < / title >
< / head >
< 正文 >
< 表单 id = form1 < span class =code-attribute> runat = server >
< div >
< table style = 宽度:100%; >
< tr >
< td > < ; asp:label id = Label3 runat = server text = 输入Id xmlns:asp = #unknown > < / asp:label > < / td >
< td > < asp:textbox id = TextBox3 runat = server xmlns:asp = #unknown > < / asp:textbox > < / td >
< / tr >
< tr >
< td > < asp:label id = Label1 runat = server text = 输入名称 xmlns:asp = #unknown > < / asp:label > < / td >
< td > < asp:textbox id = TextBox1 runat = server xmlns:asp = #unknown > < / asp:textbox > < / td >
< / tr >
< tr >
< td > < asp:label id = Label2 runat = server text = 输入电话 xmlns:asp = #unknown > < / asp:label > < / td >
< td > < asp:textbox id = TextBox2 runat = < span class =code-keyword> server
xmlns:asp = #unknown > < / asp:textbox > < / td >
< / tr >

< tr >
< td > < asp:gridview id = GridView1 runat = server xmlns:asp = #unknown > < / asp:gridview > < / td >
< td > < / td >
< / tr >
< tr >
< td > < asp:button id = Button1 runat = server text = 按钮 onclick = Button1_Click xmlns:asp = #unknown / < span class =code-keyword>> < / td >
< td > < / td >
< / tr >
< / table >
< / div >
< / form >
< / body >
< / html >



Default.aspx.cs

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
gBind();
}
}
受保护 void Button1_Click( object sender,EventArgs e)
{
updateData();
}

private void gBind()
{
string strConn = @ 数据源= LEOPALLAVI \SQLSERVER2008R2;初始目录= LOC_MASTER; UID = sa;密码= 123456;
SqlConnection conn = new SqlConnection(strConn);
尝试
{
if (conn.State == ConnectionState .Closed){conn.Open(); }
SqlCommand cmd = new SqlCommand( SELECT * FROM dbo.demosptable,conn);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (例外情况)
{
Response.Write(ex.ToString());
}
最后
{
conn.Close();
}
}

私有 void cleaall( )
{
TextBox1.Text = ;
TextBox2.Text = ;
TextBox3.Text = ;
}

private void updateData()
{
string strConn = 您的连接字符串;
SqlConnection conn = new SqlConnection(strConn);
尝试
{
if (TextBox1.Text!= < span class =code-string>

{
conn.Open();

SqlCommand cmd = new SqlCommand( demospedit4,conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter id = cmd.Parameters.Add( @ id,SqlDbType.BigInt) ;
id.Direction = ParameterDirection.InputOutput;
id.Value = TextBox3.Text;
SqlParameter name = cmd.Parameters.Add( @ name,SqlDbType.NVarChar) ;
name.Value = TextBox1.Text;
SqlParameter phone = cmd.Parameters.Add( @ phone,SqlDbType.BigInt) ;
phone.Value = TextBox2.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (例外情况)
{
Response.Write(ex.ToString( ));
}
最后
{
conn.Close();
gBind();
cleaall();
}
}


protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (TextBox1.Text != "")
            {
                conn.Open();
                
                SqlCommand cmd = new SqlCommand("demospedit4", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.BigInt);
                id.Direction = ParameterDirection.Output;
                SqlParameter name = cmd.Parameters.Add("@name", SqlDbType.NVarChar);
                name.Value = TextBox1.Text;

                SqlParameter phone = cmd.Parameters.Add("@phone", SqlDbType.BigInt);
                phone.Value = TextBox2.Text;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            conn.Close();
            gBind();
            clearall();
        }


USE [demosp]
GO
/****** Object:  StoredProcedure [dbo].[demospedit4]    Script Date: 05/29/2015 12:09:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[demospedit4](@id bigint output,@name nvarchar(20),@phone varchar(20))
as
begin
update demosptable set phone=@phone,name=@name from dbo.demosptable where id=@id
return @id
end

解决方案

You aren't giving your @id parameter a value so it has nothing to update.

SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.BigInt);
id.Direction = ParameterDirection.Output;
//id.Value = ????

SqlParameter name = cmd.Parameters.Add("@name", SqlDbType.NVarChar);
name.Value = TextBox1.Text;


Hi,

You need to pass the id value of the record which you want to update.

id	name	phone
1	madhuri	456
2	manoj	440


Please check the above table as an example :-
1. If ypu want to update phone number of 'madhuri' then you need to pass the id=1 then only your strored procedure will update the record.

2. Example :-

 SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.BigInt);
id.Direction = ParameterDirection.InputOutput;  
id.Value = TextBox3.Text;


Here in textbox 3 you can put the value 1 to update the phone number of madhuri.

Please check the below code for your reference :-
Default.aspx =

<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>]]>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 100%;">
            <tr>
                <td><asp:label id="Label3" runat="server" text="Enter Id" xmlns:asp="#unknown"></asp:label></td>
                <td> <asp:textbox id="TextBox3" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
            </tr>
            <tr>
                <td><asp:label id="Label1" runat="server" text="Enter  Name" xmlns:asp="#unknown"></asp:label></td>
                <td> <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
            </tr>
            <tr>
                <td><asp:label id="Label2" runat="server" text="Enter Phone" xmlns:asp="#unknown"></asp:label></td>
                <td> <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
            </tr>

            <tr>
                <td> <asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown"></asp:gridview> </td> 
                  <td> </td> 
            </tr>
            <tr>
                <td><asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown" /></td>
                <td> </td> 
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    { 
        updateData(); 
    } 

    private void gBind()
    {
        string strConn = @"Data Source=LEOPALLAVI\SQLSERVER2008R2;Initial Catalog=LOC_MASTER;UID=sa;password=123456";
        SqlConnection conn = new SqlConnection(strConn);
        try
        {
            if (conn.State == ConnectionState.Closed) { conn.Open(); }
            SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.demosptable", conn);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr= cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            conn.Close(); 
        }
    }

    private void cleaall()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }

    private void updateData()
    {
        string strConn = "Your connection String";
        SqlConnection conn = new SqlConnection(strConn);
        try
        {           
            if (TextBox1.Text != "")
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("demospedit4", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter id = cmd.Parameters.Add("@id", SqlDbType.BigInt);
                id.Direction = ParameterDirection.InputOutput;
                id.Value = TextBox3.Text;
                SqlParameter name = cmd.Parameters.Add("@name", SqlDbType.NVarChar);
                name.Value = TextBox1.Text;
                SqlParameter phone = cmd.Parameters.Add("@phone", SqlDbType.BigInt);
                phone.Value = TextBox2.Text;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            conn.Close();
            gBind();
            cleaall();
        }
    }


这篇关于我试图更新,但它没有解雇..什么是它的错误请帮助..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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