如何从类访问字符串数组到下拉列表项 [英] how to access string array from a class to the drop down list items

查看:74
本文介绍了如何从类访问字符串数组到下拉列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我只是创建一个类,该类提供了默认构造函数中的sql连接,作为AccessData类(AccessData.cs)
在webform1.aspx.cs
声明ddlDistrict事件并基于区域值,我必须列出该区域中的Mandal列表,该列表取自表Locationsdt(SQL Server 2008)
请向我解释如何解决此任务.

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.

推荐答案

我在这段代码中给出的几乎所有方法.但是您应该专注于此工作,并正确地分析代码.放置断点并执行.不要盲目地做.所以你什么都不能瘦.


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();
    }
}


亲爱的桑迪亚,


您可以使用Dataset或DataTable.但是Iam Ising DataTable

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;
   }


但是Infornt是要控制Web的,您应该获取这些值.对此进行分析,然后尝试.我希望您有个好主意.


But Infornt that is To Web Control You Should Retrive The values.Analize this One Try that One.I Hope You got an Idea.


尊敬的Sandhya,

Dear Sandhya,

         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)
    {
    }
}



它的作品请分析并使用您所采用的任何建筑.


问候,

Anilkumar.D



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


Regards,

Anilkumar.D


这篇关于如何从类访问字符串数组到下拉列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆