Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here i'm just creating an object to a class which provides the sql connection in the default constructor as AccessData class(AccessData.cs)
in the webform1.aspx.cs
declaring the event of ddlDistrict and based on the district value i have to list out the Mandal list in that district which is taken from the table Locationsdt(SQL server 2008)
please explain me how to solve this task.
Posted
Comments
Mehdi Gholam 27-Oct-11 1:38am    
Show your code.
sandhya.T 2011 27-Oct-11 2:01am    
sorry to say that i can't provide the code
bcoz im doing this at home, presently i'm in office.
plz help me the logic
Sergey Alexandrovich Kryukov 27-Oct-11 2:12am    
Aha, let's see... What's "object to a class"? "District value"? No punctuation... I understand next to nothing... And please, this is consider rude: "bcoz", "plz". This is textspeak, not English.
--SA
sandhya.T 2011 27-Oct-11 2:46am    
ya k but whoz dis

Almost all methods i have given in this code.But u should concentrate on this do hardwork and analize the code correctly.Put Break Points and execute.Dont do Blindly.So you Cant lean Anything.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DataAcessHelper
/// </summary>
public class DataAcessHelper
{
    SqlConnection objcon;
    public bool ExecuteSucess = false;
	public DataAcessHelper()
	{
		//
		// TODO: Add constructor logic here
		//

	}
    private void GetConnection(SqlCommand objcmd)
    {
        string connecstring = ConfigurationManager.ConnectionStrings["ANIL"].ToString();
        //string connecstring = ConfigurationManager.AppSettings["ANIL"].ToString();
        objcon = new SqlConnection(connecstring);
        objcon.Open();
        objcmd.Connection = objcon;
    }

    public int ExecuteStoredProcedure(SqlCommand objcmd)
    {
     
        GetConnection(objcmd);
        int obj = objcmd.ExecuteNonQuery();
        Dbclose(objcmd.Connection);
        ExecuteSucess = true;
        return obj;


    }
    public int ExecuteSPReturnID(SqlCommand objcmd)
    {
      
        GetConnection(objcmd);
        int tempval = Convert.ToInt32(objcmd.ExecuteScalar());
        Dbclose(objcmd.Connection);
        ExecuteSucess = true;
        return tempval;
    }
    public SqlCommand CreateCommand(string strProcedureName)
    {

        System.Data.SqlClient.SqlCommand objcmd;
        objcmd = new System.Data.SqlClient.SqlCommand();
        objcmd.CommandText = strProcedureName;
        objcmd.CommandType = CommandType.StoredProcedure;

        return objcmd;

    }
    public SqlCommand CreateTextCommand(string strquery)
    {
        System.Data.SqlClient.SqlCommand objcmd;
        objcmd = new System.Data.SqlClient.SqlCommand();
        objcmd.CommandText = strquery;
        objcmd.CommandType = CommandType.Text;
        return objcmd;
    }
    public void AddParameter(SqlCommand objCmd, string strParaname, SqlDbType enmParaType, ParameterDirection enmParaDirection, int intParaSize, object objParaValue)
    {
        System.Data.SqlClient.SqlParameter objpara;
        objpara = new System.Data.SqlClient.SqlParameter();
        objpara.ParameterName = strParaname;
        objpara.SqlDbType = enmParaType;
        objpara.Direction = enmParaDirection;
        objpara.Size = intParaSize;
        objpara.Value = objParaValue;
        objCmd.Parameters.Add(objpara);
    }
    public DataSet GetDataSet(SqlCommand objCmd)
    {
     
        GetConnection(objCmd);
        System.Data.SqlClient.SqlDataAdapter objadp;
        objadp = new System.Data.SqlClient.SqlDataAdapter(objCmd);
        DataSet ds;
        ds = new DataSet();
        objadp.Fill(ds);
        Dbclose(objCmd.Connection);
        return ds;


    }
    public DataSet GetDataSet(SqlCommand objCmd, string Tablename)
    {
        GetConnection(objCmd);
        System.Data.SqlClient.SqlDataAdapter objadp;
        objadp = new System.Data.SqlClient.SqlDataAdapter(objCmd);
        DataSet ds;
        ds = new DataSet();
        objadp.Fill(ds, Tablename);
        Dbclose(objCmd.Connection);
        return ds;
    }
    public DataSet GetDataSet(SqlCommand objCmd, int startRecord, int MaxRecord, string srcTable)
    {
        GetConnection(objCmd);
        System.Data.SqlClient.SqlDataAdapter objadp;
        objadp = new System.Data.SqlClient.SqlDataAdapter(objCmd);
        DataSet ds;
        ds = new DataSet();
        objadp.Fill(ds);
        Dbclose(objCmd.Connection);
        return ds;

    }


    public DataTable GetDataTable(SqlCommand objCmd)
    {
        GetConnection(objCmd);
        System.Data.SqlClient.SqlDataAdapter objadp;
        objadp = new System.Data.SqlClient.SqlDataAdapter(objCmd);
        DataTable dt;
        dt = new DataTable();
        objadp.Fill(dt);
        Dbclose(objCmd.Connection);
        return dt;
    }
    public object GetValue(SqlCommand objCmd)
    {
    
        GetConnection(objCmd);
        object objvalue;
        objvalue = objCmd.ExecuteScalar();
        Dbclose(objCmd.Connection);
        return objvalue;
    }
    public bool DataExists(SqlCommand objcmd)
    {
        bool blnStatus;
        DataTable dtExist = GetDataTable(objcmd);
        if (dtExist.Rows.Count > 0)
        {
            blnStatus = true;
        }
        else
        {
            blnStatus = false;
        }
        return blnStatus;

    }
    private void Dbclose(SqlConnection sqlcon)
    {
        sqlcon.Close();
        sqlcon.Dispose();
    }
}
 
Share this answer
 
v2
Comments
sandhya.T 2011 31-Oct-11 5:46am    
k I'll go through the each block and method
Thank u so much anil.
it helps me a lot.
and one more thing I'm getting an error as " A network related error occurred while connecting to the data source" how can i solve this problem...
try{
SqlConnection con= new SqlConnection("Data Source=XP\SqlServer; Data Base= ApplicationDB; Integrated Security=true;");
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
please help me, I'm not able to detect it???????
Anil Honey 206 31-Oct-11 7:35am    
You try like this.
private string conString = "Data Source=XP\SqlServer; Data Base= ApplicationDB; Integrated Security=true;";

SqlConnection sqlCon;

sqlCon = new SqlConnection(conString);

it will work
Anil Honey 206 31-Oct-11 7:36am    
in your code you have taken sqlnamespace
using System.Data;
using System.Data.SqlClient;

you include this namespaces
sandhya.T 2011 31-Oct-11 8:23am    
yes i have included after that also i'm getting the error?
through the ado.net i'm getting the results by using the ADO.net but im not connected by these stmts?
Anil Honey 206 31-Oct-11 23:43pm    
i think that connectionstring path is not correct check once.
Dear Sandhya,


You can Use Dataset or DataTable.But iam Ising DataTable

public DataTable GetMantal()
   {
       DataTable dt = new DataTable();

       DataAcessHelper dashp = new DataAcessHelper();
       SqlCommand cmd = dashp.CreateCommand("sp_getMandal);
       dashp.AddParameter(cmd, "@districtname", SqlDbType.VarChar, ParameterDirection.Input, 50, FullName);
       dt = dashp.GetDataTable(cmd);

       return dt;
   }

But Infornt that is To Web Control You Should Retrive The values.Analize this One Try that One.I Hope You got an Idea.
 
Share this answer
 
v2
Comments
sandhya.T 2011 2-Nov-11 2:04am    
yes i got an idea basis on this code
thank u so much anil for advising from the starting point to the concluding part,
thank u so much n have a nice day
Anil Honey 206 2-Nov-11 2:23am    
Same to You Have a Nice Day.
Dear Sandhya,

C#
         protected void cmbDist_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        ds = new DataSet();
        ad = new SqlDataAdapter("select intmandalid,strMandalename from Mandal where intDiatrictid=" + cmbDist.SelectedItem.Value + "", (SqlConnection)Application.Get("JDCI"));
        ad.Fill(ds);
        drpmandal.Items.Clear();
        drpmandal.Items.Add("---Select---");
        drpmandal.Items[0].Value = "0";
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            drpmandal.Items.Add(ds.Tables[0].Rows[i][1].ToString());
            drpmandal.Items[i + 1].Value = ds.Tables[0].Rows[i][0].ToString();
        }
    }
    catch (Exception ex)
    {
    }
}


Its Works Please analize and do with what ever architecture u have taken.


Regards,

Anilkumar.D
 
Share this answer
 
v2
Comments
sandhya.T 2011 27-Oct-11 2:30am    
thanq for ur reply
but it is the webform page i just want to access the values from a seperate class
protected void cmbDist_SelectedIndexChanged(object sender, EventArgs e)
{

}
sandhya.T 2011 27-Oct-11 2:37am    
protected void cmbDist_SelectedIndexChanged(object sender, EventArgs e)
{
AccessData c1= new AccessData();
string[] M=c1.Mandal(ddlDistrict.value);//how can i pass an argument
without using a parameter for the value of the ddl district
//by using the above stmt string[] can be retrieved or not
}
AccessData.cs
AccessData()
{
//connection stmts of sql server
}
public string[]Mandal(string dis)
{
//here i want to return the string array i.e list of mandals
}

please help me
Anil Honey 206 27-Oct-11 2:53am    
What architecture ur using 3tier or 2tier Ur not setting any properties in your class file.
sandhya.T 2011 27-Oct-11 3:54am    
hi archana,
im dng a simple project using a VS.net 2010 with the Sql server 2008,
now i have referred these terms then i got this info.
i.e im using the 2 tier application but i don't know the depth of it
Anil Honey 206 27-Oct-11 4:17am    
Ur using Stored Procedures in your Class file.If your Using Class file then you should set properties for that means if for district you sholud give one propert for that and also for Mandal then u can pass the values and get the values.
Here iam giving the Code You Should analize the code some sample iam giving.

C#
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

public class District
{
	public InboxCount()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    private Int32 _intDistrictid;
    private string strDistrictname;

    public Int32 IntDistrictId
    {
        get { return _intDistrictid; }
        set { _intDistrictid= value; }
    }

    public string DistrictName
    {
        get { return strDistrictname; }
        set { strDistrictname= value; }
    }

    //Example

    public DataTable InsertExample()
    {
        DataTable dt = new DataTable();

        DataAcessHelper acess = new DataAcessHelper();
        SqlCommand cmd = acess.CreateCommand("sp_stroreDistrict");
        acess.AddParameter(cmd, "@DistrictId", SqlDbType.Int, ParameterDirection.Input, 4, IntDistrictId);
        acess.AddParameter(cmd, "@Districtname", SqlDbType.VarChar, ParameterDirection.Input, 50, DistrictName);
        acess.ExecuteStoredProcedure(cmd);

        return dt;
    }

     public DataTable GetTotalMessage()
    {
        DataTable dt = new DataTable();

        DataAcessHelper dashp = new DataAcessHelper();
        SqlCommand cmd = dashp.CreateCommand("Sp_getdistrict");
        dashp.AddParameter(cmd, "@DistrictId", SqlDbType.Int, ParameterDirection.Input, 4, IntDistrictId);
        dt = dashp.GetDataTable(cmd);

        return dt;
    }
    public DataTable DeleteInbox()
    {
        DataTable dt = new DataTable();

        DataAcessHelper dashp = new DataAcessHelper();
        SqlCommand cmd = dashp.CreateCommand("sp_Deletedistrict");
        dashp.AddParameter(cmd, "@DistrictId", SqlDbType.Int, ParameterDirection.Input, 4, IntDistrictId);
        dashp.ExecuteStoredProcedure(cmd);


        return dt;
    }

}



You Should analize everything then only You can understand.
 
Share this answer
 
v3
Comments
Anil Honey 206 27-Oct-11 5:15am    
For this you should have DataacessHelper class that is Dal u have that one
sandhya.T 2011 27-Oct-11 5:33am    
Thanq i'l go through it
Dal class is a predefined ?
sandhya.T 2011 28-Oct-11 0:39am    
i wrote that using System.Dal; //stmt
i'm getting this error as does not exist the defined namespace
i have referred in the add references liat also but i din't find that namespace
how to add this class or namespace
please can u explain it?
Anil Honey 206 28-Oct-11 0:59am    
Dear Friend Dal is Not A Predifined class you should define your Own Class for that.Your Going to Implement Three tire Architecture.So frst you search through Google How to implement dataacess layer in your Project.
sandhya.T 2011 28-Oct-11 1:10am    
offo!!!
k k i'l try my level best to get the concept thank u for your advice archana.
Dear Friend,

This is Simple Dataacess layer first Analize this and Implement in Your Project get and idea About This.Ok.

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;

/// <summary>
/// Summary description for DataBaseClass
/// </summary>
public class DataBaseClass
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ToString());

	public DataBaseClass()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public DataSet GetDataset(string strQuery)
    {
        try
        {
            SqlDataAdapter da = new SqlDataAdapter(strQuery, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public SqlDataReader ExecuteReader(string strQuery)
    {
        try
        {
            SqlDataReader dr = null;
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            else if (con.State == ConnectionState.Broken)
            {
                con.Close();
                con.Open();
            }
            SqlCommand cmd = new SqlCommand(strQuery, con);
            dr = cmd.ExecuteReader();
            return dr;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public object ExecuteScalar(string strQuery)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            else if (con.State == ConnectionState.Broken)
            {
                con.Close();
                con.Open();
            }
            SqlCommand cmd = new SqlCommand(strQuery, con);
            return cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void ExecuteNonQuery(string strQuery)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            else if (con.State == ConnectionState.Broken)
            {
                con.Close();
                con.Open();
            }

            SqlCommand cmd = new SqlCommand(strQuery,con);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    //internal DataSet GetDataSet(string strQuery)
    //{
    //    throw new NotImplementedException();
    //}
}
 
Share this answer
 
v2
Comments
sandhya.T 2011 31-Oct-11 2:48am    
is it means overriding the methods which are predefined methods according to our project requirements
Anil Honey 206 31-Oct-11 4:18am    
S like we can make our methods inside class file.We can use lot of methods in it.
sandhya.T 2011 31-Oct-11 4:20am    
k..
here in the above code we are passing the strQuery as command or it is a stored procedure name?
Anil Honey 206 31-Oct-11 4:19am    
This code u can pass queries.If u want to pass stored Procedure.YOU should change the methods.
sandhya.T 2011 31-Oct-11 4:24am    
how can i change those methods.
should i pass the stored procedure names by directly.
please can u give me an example for it

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