Anil Kumar

Anil Kumar

  • 712
  • 1.3k
  • 134.1k

Please solve this problem

Jun 28 2011 8:33 AM
Hi, i have create a web form..and i have use Add,view and update data accourding to this,i have done all code in cs file,but i want to do all with the help of store procedure..please solve this accourding Store procedure
 

ASPX code:-

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

<!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>
        <table>
            <tr>
                <td style="width: 100px; height: 21px">
                    Name</td>
                <td style="width: 100px; height: 21px">
                    <asp:TextBox ID="txt_name" runat="server"></asp:TextBox></td>
                <td style="width: 100px; height: 21px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Mobile No.</td>
                <td style="width: 100px">
                    <asp:TextBox ID="txt_mobileNo" runat="server"></asp:TextBox></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px; height: 26px">
                    <asp:Button ID="btn_Add" runat="server" OnClick="btn_Add_Click" Text="Add" /></td>
                <td style="width: 100px; height: 26px">
                    <asp:Button ID="btn_View" runat="server" Text="View" OnClick="btn_View_Click" /></td>
                <td style="width: 100px; height: 26px">
                    <asp:Button ID="btn_update" runat="server" Text="Update" OnClick="btn_update_Click" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

CS code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(@"Data source=TECHID\SQLEXPRESS;initial catalog=test_abdhesh;integrated security=true");
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    void clear()
    {
        txt_mobileNo.Text = "";
        txt_name.Text = "";
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into phone_book values('" + txt_name.Text.ToString() + "','" + txt_mobileNo.Text.ToString() + "')", con);
            cmd.ExecuteNonQuery();
            clear();
        }
        catch (Exception ex)
        {
            Response.Write("This Mobile No. alreay Exist");
        }
        finally
        {
            con.Close();
        }
        
    }
    protected void btn_View_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select P_name, P_no from phone_book where P_no='" + txt_mobileNo.Text.ToString() + "'", con);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                txt_name.Text = dr[0].ToString();
                txt_mobileNo.Text = dr[1].ToString();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void btn_update_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("update phone_book set P_name='" + txt_name.Text.ToString() + "' where P_no='" + txt_mobileNo.Text.ToString() + "'", con);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);

        }
        finally
        {
            con.Close();

        }
    }
}




Answers (2)