1
Reply

not stored values in database with using stored procedure

JAYRAM

JAYRAM

Apr 26 2012 11:28 AM
2.1k
plz tell me any wrong this code.....error inserting the values in database

business layer


using System;
using System.Data;
using System.Configuration;
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;
using Cmplogistics.DAL;

namespace Cmplogistics.BAL
{
    public class Menucls
    {
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adp = new SqlDataAdapter();

        private int M_ID;
        public int MenuID
        {
            set { M_ID = value; }
            get { return M_ID; }
        }

        private string M_NAME;
        public string MenuName
        {
            set { M_NAME = value; }
            get { return M_NAME; }
        }

        private string M_PAGEURL;
        public string MenuPageURL
        {
            set { M_PAGEURL = value; }
            get { return M_PAGEURL; }
        }

        private string M_ORDER;
        public string MenuOrder
        {
            set { M_ORDER = value; }
            get { return M_ORDER; }
        }

        public bool insertmenu()
        {

            try
            {
                string str = "SP_MENUINSERT";
                Cmplogistics.DAL.Dataaccess obj = new Cmplogistics.DAL.Dataaccess();
                SqlParameter[] p = new SqlParameter[4];
                p[0] = new SqlParameter("@M_ID", M_ID);
                p[1] = new SqlParameter("@M_NAME", M_NAME);
                p[2] = new SqlParameter("@M_PAGEURL", M_PAGEURL);
                p[3] = new SqlParameter("@M_ORDER", M_ORDER);
                obj.insertstored(str, p);
                return true;

            }
            catch (Exception ex)
            {
                throw ex;



                //adp.SelectCommand = cmd;
                //bool result= obj.insertstored(str);
                //obj.trans.Commit();
                //obj.Dispose();
                //return result;
            }


            
        }
    }
}


dataacess layer

using System;
using System.Data;
using System.Configuration;
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;

namespace Cmplogistics.DAL
{
    public class Dataaccess
    {
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adp = new SqlDataAdapter();
            public SqlTransaction trans;
            DataTable dt = new DataTable();
            string sConnect = "Data Source=SHASHI-PC;Initial Catalog=DLS;Integrated Security=True";

            public Dataaccess()
            {
                con.ConnectionString = sConnect;
                try
                {

                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    trans = con.BeginTransaction(IsolationLevel.ReadCommitted);
                    cmd.Connection = con;
                    cmd.Transaction = trans;
                }
                catch (Exception ex)
                {
                    throw ex;

                }
            }

            // Insert Data

            public bool insertdata(string insertstmt)
            {
                try
                {
                    cmd.CommandText = insertstmt;
                    adp.SelectCommand = cmd;
                    adp.Fill(dt);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            public bool insertstored(string insertstmt,SqlParameter[] commandparameters)
            {
                try
                {
                   
                    cmd.CommandText = insertstmt;
                    cmd.CommandType = CommandType.StoredProcedure;
                    //cmd.Parameters.AddWithValue("@M_ID");
                    //cmd.Parameters.AddWithValue("@M_NAME");
                    //cmd.Parameters.AddWithValue("@M_PAGEURL", "Home.aspx");
                    //cmd.Parameters.AddWithValue("@M_ORDER", 001);
                  
                    foreach(SqlParameter p in commandparameters)
                    {
                        if((p.Direction==ParameterDirection.InputOutput) && (p.Value==null))
                        {
                            p.Value= DBNull.Value;
                        }
                        cmd.Parameters.Add(p);
                    }
                    cmd.ExecuteNonQuery();
                        return true;
                }
                catch (Exception ex)
                {
                    return false;
                }

            }


      public void Dispose()
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

            }

        }
    
}



button 


 protected void Button1_Click(object sender, EventArgs e)
        {
            Cmplogistics.BAL.Menucls obj = new Cmplogistics.BAL.Menucls();
            obj.MenuID = Convert.ToInt32(TextBox1.Text);
            obj.MenuName = TextBox2.Text.Trim();
            obj.MenuPageURL = TextBox3.Text.Trim();
            obj.MenuOrder = TextBox4.Text.Trim();
            bool result = obj.insertmenu();
            Response.Write("Menu Inserted");

        }









Attachment: cmplogistics.rar

Answers (1)