Translate

Saturday 14 February 2015

sql Procedure with ROLLBACK TRAN

Simply Copy and past it in ur Sql server and webform it will work
------------------------------------------------------------------

Write this code in Sql
--------------------------

CREATE TABLE tblEmp
(
em_Id int NOT NULL primary key,
em_Name varchar(200) NULL
)


---------------------------------------

CREATE PROCEDURE tblEmpInsert --1001,'pabitramicrosoftreasearch.blogspot.com'
    -- Add the parameters for the stored procedure here
@em_Id        int,
@em_Name      varchar(200)=NULL,
@msg      varchar(200)=NULL  output
AS
BEGIN --Starts begin
Declare  @errorDueto INT ---Declare For Error Checking
   
SET NOCOUNT ON;---Declare For not Count the rows affected
BEGIN TRAN Mytransatation --Mytransatation = Is likea Alias

    SET @errorDueto = 0 
    Insert into tblEmp(em_Id,em_Name) values(@em_Id,@em_Name)
   
   
     IF (@@ERROR <> 0)
     SET @errorDueto = 100
   
     IF (@@ERROR = 0)        
     SET @msg='Emplyee Save Successfully'
     ELSE  
     SET @msg=' faild due to '+@errorDueto



    IF(@errorDueto <> 0)
    BEGIN
    ROLLBACK TRAN Mytransatation
    RETURN @errorDueto
    END
    ELSE
    COMMIT TRAN Mytransatation
end ------end of begin
-----------------------------------------------

MyEmployee Design Page
---------------------------------------


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
     <script type="text/javascript">
         function Validate() {
             if (!CheckValidate("<%=txtEmpID.ClientID %>", "Employee ID")) {
                document.getElementById("<%=txtEmpID.ClientID %>").style.backgroundColor = "red";
                return false;
            }
             if (!CheckValidate("<%=txtEmpName.ClientID %>", "Employee Name")) {
                document.getElementById("<%=txtEmpName.ClientID %>").style.backgroundColor = "red";
                return false;
            }

        }
    </script>
   <script type="text/javascript">
    function CheckValidate(ControlID, msg)
           {
               var  VarControlID = document.getElementById(ControlID);
               if (VarControlID.value == "")
                    {
                      alert(msg + ' cannot be  Blank!');
                      VarControlID.focus();
                      return false;
                    }
          return true;
        }
   </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        Employee ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;
        <asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>
        <br />
        Employee Name&nbsp;&nbsp; :&nbsp;
        <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
        <br />
&nbsp;&nbsp;&nbsp;
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnSave" OnClientClick="return Validate();" runat="server" Text="Save" OnClick="btnSave_Click" />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
   
    </div>
    </form>
</body>
</html>
------------------------------------------------------------------------------------------
My Class file
-------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class EmployeeDtl : System.Web.UI.Page
{
    string SqlCon = "server=.;uid=sa;pwd=pabitramicrosoft;database=master";
     //or
   // string Sqlconn=System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        try
        {
            string Message = "";
            con = new SqlConnection(SqlCon);
            cmd = new SqlCommand("tblEmpInsert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@em_Id", Convert.ToInt32(txtEmpID.Text));
           // cmd.Parameters.AddWithValue("@em_Id", "qq");
            //cmd.Parameters.AddWithValue("@em_Id", "133.345");
            cmd.Parameters.AddWithValue("@em_Name", txtEmpName.Text);
            SqlParameter p = new SqlParameter("@msg", SqlDbType.VarChar, 8000);
            p.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(p);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd.ExecuteNonQuery();
            Message = (string)cmd.Parameters["@msg"].Value;
            con.Close();
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + Message + "');", true);
        }
        catch (SqlException ex)
        {
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('"+ex.Message+"');", true);
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + ex.Message+ "');", true);
        }
    }
}
-------------------------------------------------------------------------------------------------------------------

Thursday 5 February 2015

split string assign in gridview

 protected void btnShow_Click(object sender, EventArgs e)
    {
        string value = "Pabitra,Pabitra2,Pabitra3,Pabitra4,Pabitra5";
       // using System.Text.RegularExpressions;
       // string[] lines = Regex.Split(value, ",");
        string[] lines = value.Split(',');
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        foreach (string line in lines)
        {
            DataRow dr = dt.NewRow();
            dr["Name"] = line;
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

Monday 2 February 2015

how to find control in gridview rowcommand in asp.net c# ?

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Find"))
        {
           One Way(1)
          ----------
                     // int indexno = int.Parse(e.CommandArgument.ToString());
                     // GridViewRow row = GridView1.Rows[indexno ];
          One Way(2)
          ----------
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);                 Label SubCatID = (Label)row.FindControl("lblSubCatID");
            string Sub = SubCatID.Text;      
       
        }
    }    

Procedure Error Handleing

create proc MyProcedure
(
@Emp_Id int,
@Name nvarchar(50),
@Action varchar(3),
@message nvarchar(100) output
)
as
begin
 if(@Action='U')
 begin
  update tblEMP set E_name=@Name where Emp_Id=@Emp_Id  
 end    
 if(@@ERROR=0)
  set @message='Save successfully.'
 else
  set @message='Error in deletion.'
end  

Dialog box in C$ web app

using System.Windows.Forms;
--------------------------------------------
 int x=Insert(txtId.Text,txtName.Text);

if (MessageBox.Show(msg+"  Do you want Add another In The same species .. ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                   
                }else

{

}