Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
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();
        }

SQL
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
Posted
Updated 1-Jun-15 0:38am
v2
Comments
[no name] 29-May-15 2:56am    
is it showing any specific error??
Member 11727380 29-May-15 3:13am    
NO doesn"t show any error..just just when I click the update button
Aditya Chauhan 29-May-15 3:44am    
we ask what error you got ?
Member 11727380 29-May-15 3:57am    
there is no error... it doesn't update

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

SQL
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;
 
Share this answer
 
v3
Comments
Member 11727380 29-May-15 3:53am    
can you please tell me what should i put in id.value=
F-ES Sitecore 29-May-15 4:18am    
The ID of the record you are trying to update.
Hi,

You need to pass the id value of the record which you want to update.
SQL
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 :-
C#
 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 =
C#
<![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
C#
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();
        }
    }
 
Share this answer
 

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