Click here to Skip to main content
16,007,610 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: directory delete Pin
Vasudevan Deepak Kumar19-May-08 18:50
Vasudevan Deepak Kumar19-May-08 18:50 
Questionthis error occure in my function:not all code paths return a value Pin
Saba0219-May-08 2:27
Saba0219-May-08 2:27 
AnswerRe: this error occure in my function:not all code paths return a value Pin
MHASSANF19-May-08 2:32
MHASSANF19-May-08 2:32 
AnswerRe: this error occure in my function:not all code paths return a value Pin
l0kke19-May-08 2:48
l0kke19-May-08 2:48 
QuestionError inserting records into MySql table using Asp 2.0 [modified] Pin
mlingamus19-May-08 2:01
mlingamus19-May-08 2:01 
AnswerRe: Error inserting records into MySql table using Asp 2.0 Pin
doWhileSomething19-May-08 15:10
doWhileSomething19-May-08 15:10 
QuestionFormat the value in report Pin
member2719-May-08 1:44
member2719-May-08 1:44 
QuestionI need ur views to make improvements in the DataBase Interaction file. Pin
Pankaj Garg19-May-08 0:38
Pankaj Garg19-May-08 0:38 
Hi,
I made a class file , which is being used to interact with the database.
Can i get ur views to improve it.
I m doing all the rollback /commit transaction at the stored procedure level.
According to me this is my Best code ever written in my carrier.
Here is the code.....

using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Web.UI.HtmlControls;<br />
using System.Data.SqlClient;<br />
<br />
namespace MyClasses<br />
{<br />
    public class AddParameters<br />
    {  <br />
        public AddParameters[] p;<br />
        internal SqlParameter param = new SqlParameter();<br />
        public AddParameters()<br />
        {<br />
            p = new AddParameters[1];<br />
        }<br />
        public void ParameterFunction&lt;T&gt;(string ParameterName, T _ParameterValue, ParameterDirection ParameterDirection, SqlDbType ParameterType)<br />
        {<br />
            if (p[p.Length - 1] != null)<br />
            {<br />
                AddParameters[] temp = new AddParameters[p.Length];<br />
                p.CopyTo(temp, 0);<br />
                p = new AddParameters[p.Length + 1];<br />
                for (int i = 0; i &lt; p.Length - 1; i++)<br />
                {<br />
                    p[i] = temp[i];<br />
                }<br />
                p[p.Length - 1] = new AddParameters();<br />
                p[p.Length - 1].param.ParameterName = ParameterName;<br />
                p[p.Length - 1].param.Value = _ParameterValue;<br />
                p[p.Length - 1].param.Direction = ParameterDirection;<br />
                p[p.Length - 1].param.SqlDbType = ParameterType;<br />
            }<br />
            else<br />
            {<br />
                p[0] = new AddParameters();<br />
                p[0].param.ParameterName = ParameterName;<br />
                p[0].param.SqlValue = _ParameterValue;<br />
                p[0].param.Direction = ParameterDirection;<br />
                p[0].param.SqlDbType = ParameterType;<br />
            }<br />
        }<br />
    }<br />
<br />
    public class DatabaseInteraction//:AddParameters<br />
    {<br />
        AddParameters ad = null;<br />
        SqlConnection con = null;<br />
        private string connectionstring = "";<br />
        private string StoredProcedure = "";<br />
        SqlCommand cmd = null;<br />
        DataSet ds = null;<br />
        SqlDataAdapter sqlad = null;<br />
        public void AddParameters&lt;T&gt;(string Parametername, T Parametervalue, ParameterDirection ParameterDirection, SqlDbType type)<br />
        {<br />
        //  ad.ParameterFunction(Parametername, Parametervalue, ParameterDirection, type);<br />
            //this.p = ad.p;<br />
        }<br />
        public DatabaseInteraction()<br />
        {<br />
            <br />
        }<br />
        public static string ChangeDateFormat(string t)<br />
        {<br />
            string dat = "";<br />
            DateTime datformat;<br />
            if (DateTime.TryParse(t, out datformat))<br />
            {<br />
                dat = datformat.ToString("dd/MM/yyyy");<br />
                return dat;<br />
            }<br />
            return dat;<br />
        }<br />
        public DatabaseInteraction(string constr)<br />
        {<br />
            connectionstring = constr;<br />
            ad = new AddParameters();<br />
        }  <br />
      <br />
        private string Connect(out SqlConnection con)<br />
        {<br />
            try<br />
            {<br />
                con = new SqlConnection(connectionstring);<br />
                con.Open();<br />
                return "";<br />
            }<br />
<br />
            catch (Exception ex)<br />
            {<br />
                con = null;<br />
                return ex.Message.ToString();<br />
            }<br />
        }<br />
      <br />
        public string StoredProcedureName<br />
        {            <br />
            get<br />
            {<br />
                return StoredProcedure;<br />
            }<br />
            set<br />
            {<br />
                StoredProcedure = value;<br />
            }<br />
        }<br />
        public string BindingWithControl(Control ctl, string ValueMember, string DisplayMember)<br />
        {<br />
            string message = Connect(out con);<br />
            if (message == "")<br />
            {<br />
                message = GetDataSet(con, ref ds);<br />
                if (message == "")<br />
                {<br />
                    BindControl(ctl, ds, DisplayMember, ValueMember);<br />
                    con.Close();<br />
                    return "";<br />
                }<br />
                else<br />
                {<br />
                    con.Close();<br />
                    return message;<br />
                }<br />
            }<br />
            else<br />
            {<br />
                con.Close();<br />
                return message;<br />
            }<br />
        }<br />
<br />
        public void BindControl(Control ctl, DataSet ds, string DisplayMember, string ValueMember)<br />
        {<br />
            if (ctl is RadioButtonList)<br />
            {<br />
                RadioButtonList chk;<br />
                chk = (RadioButtonList)ctl;<br />
                chk.DataTextField = DisplayMember;<br />
                chk.DataValueField = ValueMember;<br />
                chk.DataSource = ds.Tables[0];<br />
                chk.DataBind();<br />
            }<br />
            if (ctl is CheckBoxList)<br />
            {<br />
                CheckBoxList chk;<br />
                chk = (CheckBoxList)ctl;<br />
                chk.DataTextField = DisplayMember;<br />
                chk.DataValueField = ValueMember;<br />
                chk.DataSource = ds.Tables[0];<br />
                chk.DataBind();<br />
            }<br />
            if (ctl is DropDownList)<br />
            {<br />
                DropDownList ddl;<br />
                ddl = (DropDownList)ctl;<br />
                //      ddl.Items.Clear();<br />
                if (DisplayMember != "")<br />
                {<br />
                    ddl.DataTextField = DisplayMember;<br />
                }<br />
                if (ValueMember != "")<br />
                {<br />
                    ddl.DataValueField = ValueMember;<br />
                }              <br />
                ddl.DataSource = ds;<br />
                ddl.DataBind();<br />
            }<br />
            if (ctl is GridView)<br />
            {<br />
                GridView gv;<br />
                gv = (GridView)ctl;<br />
                if (ds.Tables.Count == 0)<br />
                {<br />
                    return;<br />
                }<br />
                gv.ID = ctl.ID;<br />
                gv.DataSource = ds;<br />
                gv.DataBind();<br />
            }<br />
            if (ctl is Repeater)<br />
            {<br />
                Repeater gv;<br />
                gv = (Repeater)ctl;<br />
                if (ds.Tables.Count == 0)<br />
                {<br />
                    return;<br />
                }<br />
                gv.ID = ctl.ID;<br />
                gv.DataSource = ds;<br />
                gv.DataBind();<br />
            }<br />
            if (ctl is DataList)<br />
            {<br />
                DataList gv;<br />
                gv = (DataList)ctl;<br />
                if (ds.Tables.Count == 0)<br />
                {<br />
                    return;<br />
                }<br />
                gv.ID = ctl.ID;<br />
                gv.DataSource = ds;<br />
                gv.DataBind();<br />
            }<br />
        }<br />
<br />
        public string GetDataSet(SqlConnection con, ref DataSet ds)<br />
        {<br />
            try<br />
            {<br />
                <br />
                cmd = new SqlCommand();<br />
                cmd.Connection = con;<br />
                cmd.CommandType = CommandType.StoredProcedure;<br />
                cmd.CommandText = StoredProcedure;<br />
                if (ad != null)<br />
                {<br />
                    if (ad.p[0] != null)<br />
                    {<br />
                        if (ad.p.Length != 0)<br />
                            ReturnSqlCommand(ref cmd, ad.p);<br />
                    }<br />
                }<br />
                ds = new DataSet();<br />
                <br />
                sqlad = new SqlDataAdapter(cmd);<br />
                sqlad.Fill(ds);<br />
                if (ad.p[0] == null)<br />
                {<br />
                    return "";<br />
                }<br />
                for (int count = 0; count &lt; ad.p.Length; count++)<br />
                {<br />
                    if (ad.p[count] == null)<br />
                        continue;<br />
                    if (ad.p[count].param.Direction == ParameterDirection.Output)<br />
                    {<br />
                        ad.p[count].param.SqlValue = cmd.Parameters[count].Value.ToString();<br />
                    }<br />
                    if (ad.p[count].param.Direction == ParameterDirection.InputOutput)<br />
                    {<br />
                        ad.p[count].param.SqlValue = cmd.Parameters[count].Value.ToString();<br />
                    }<br />
                }                              <br />
                return "";<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                return ex.Message.ToString();                <br />
            }<br />
        }<br />
  <br />
        private SqlCommand ReturnSqlCommand(ref SqlCommand cmd, AddParameters[] db)<br />
        {<br />
            for (int count = 0; count &lt; db.Length; count++)<br />
            {<br />
                if (db[count].param.Direction == ParameterDirection.Input)<br />
                {<br />
                    cmd.Parameters.AddWithValue(db[count].param.ParameterName, db[count].param.SqlDbType).Value = db[count].param.SqlValue;<br />
                    cmd.Parameters[count].SqlDbType = db[count].param.SqlDbType;<br />
                }<br />
                if (db[count].param.Direction == ParameterDirection.Output)<br />
                {<br />
                    cmd.Parameters.AddWithValue(db[count].param.ParameterName, db[count].param.SqlDbType);<br />
                    cmd.Parameters[count].SqlDbType = db[count].param.SqlDbType;<br />
                    cmd.Parameters[count].Direction = ParameterDirection.Output;<br />
                }<br />
                if (db[count].param.Direction == ParameterDirection.InputOutput)<br />
                {<br />
                    cmd.Parameters.AddWithValue(db[count].param.ParameterName, db[count].param.SqlDbType);<br />
                    cmd.Parameters[count].SqlDbType = db[count].param.SqlDbType;<br />
                    cmd.Parameters[count].Direction = ParameterDirection.InputOutput;<br />
                    cmd.Parameters[count].Size = 3000;<br />
                }<br />
            }<br />
            return cmd;<br />
        }<br />
<br />
        public string returnScalarData()<br />
        {<br />
            SqlCommand cmd = new SqlCommand();<br />
            SqlConnection con;<br />
            Connect(out con);<br />
            DataSet ds = new DataSet();<br />
            cmd.Connection = con;<br />
            cmd.CommandType = CommandType.StoredProcedure;<br />
            cmd.CommandText = StoredProcedure;<br />
            if (ad != null)<br />
            {<br />
                if (ad.p != null)<br />
                {<br />
                    if (ad.p.Length != 0)<br />
                        ReturnSqlCommand(ref cmd, ad.p);<br />
                }<br />
                <br />
            }<br />
            if (cmd.ExecuteScalar() == null)<br />
                return null;<br />
            else<br />
                return cmd.ExecuteScalar().ToString().Trim();<br />
        }<br />
<br />
        public DataSet returndataset()<br />
        {<br />
            SqlCommand cmd = new SqlCommand();<br />
            SqlConnection con;<br />
            Connect(out con);<br />
            DataSet ds = new DataSet();<br />
            cmd.Connection = con;<br />
            cmd.CommandType = CommandType.StoredProcedure;<br />
            cmd.CommandText = StoredProcedure;<br />
            if (ad != null)<br />
            {<br />
                if (ad.p[0] != null)<br />
                {<br />
                    if (ad.p.Length != 0)<br />
                        ReturnSqlCommand(ref cmd, ad.p);<br />
                }<br />
            }<br />
            sqlad = new SqlDataAdapter(cmd);<br />
            sqlad.Fill(ds);<br />
            sqlad = null;<br />
            return ds;<br />
        }<br />
    }<br />
}


If you have an apple & I have an apple and we exchange our apples, then each of us will still have only one apple but if you have an idea & I have an idea and we exchange our ideas, then each of us will have two ideas!

AnswerRe: I need ur views to make improvements in the DataBase Interaction file. Pin
Vasudevan Deepak Kumar19-May-08 0:51
Vasudevan Deepak Kumar19-May-08 0:51 
GeneralRe: I need ur views to make improvements in the DataBase Interaction file. Pin
Pankaj Garg19-May-08 0:55
Pankaj Garg19-May-08 0:55 
GeneralRe: I need ur views to make improvements in the DataBase Interaction file. Pin
Vasudevan Deepak Kumar19-May-08 18:49
Vasudevan Deepak Kumar19-May-08 18:49 
AnswerRe: I need ur views to make improvements in the DataBase Interaction file. Pin
N a v a n e e t h19-May-08 1:20
N a v a n e e t h19-May-08 1:20 
AnswerRe: I need ur views to make improvements in the DataBase Interaction file. [modified] Pin
J4amieC19-May-08 1:40
J4amieC19-May-08 1:40 
QuestionCompiler Error Message: CS0246: The type or namespace name 'XYZ' could not be found ...... Pin
Aldorado19-May-08 0:20
Aldorado19-May-08 0:20 
QuestionHow to use button control inside ModalPopup Ajax Control Pin
girishdonde18-May-08 23:59
girishdonde18-May-08 23:59 
AnswerRe: How to use button control inside ModalPopup Ajax Control [modified] Pin
Vinay Dornala19-May-08 1:39
Vinay Dornala19-May-08 1:39 
QuestionHow i can resolve this error? Pin
Saba0218-May-08 23:35
Saba0218-May-08 23:35 
AnswerRe: How i can resolve this error? Pin
Saba0218-May-08 23:45
Saba0218-May-08 23:45 
GeneralRe: How i can resolve this error? Pin
Vasudevan Deepak Kumar19-May-08 0:16
Vasudevan Deepak Kumar19-May-08 0:16 
AnswerRe: How i can resolve this error? Pin
Aldorado19-May-08 0:24
Aldorado19-May-08 0:24 
AnswerRe: How i can resolve this error? Pin
Pankaj Garg19-May-08 1:02
Pankaj Garg19-May-08 1:02 
QuestionHow i can resolve this error? Pin
Saba0218-May-08 23:30
Saba0218-May-08 23:30 
AnswerRe: How i can resolve this error? Pin
eyeseetee18-May-08 23:35
eyeseetee18-May-08 23:35 
AnswerRe: How i can resolve this error? Pin
Vasudevan Deepak Kumar19-May-08 0:46
Vasudevan Deepak Kumar19-May-08 0:46 
AnswerRe: How i can resolve this error? Pin
N a v a n e e t h19-May-08 1:26
N a v a n e e t h19-May-08 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.