locked
IListSource does not contain any data sources. RRS feed

  • Question

  • User-1061668677 posted

    I'm pretty new to C# .net programming and am having a hard time getting this right. What am I missing?

    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;
    public partial class _Default : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        //Here we declare the parameter which we have to use in our application
        SqlCommand cmd = new SqlCommand();
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    
        public static void ExecuteNonQueryStoredProcedure(string ProcedureName, params SqlParameter[] values)
        {
            SqlConnection conn;
            conn = new SqlConnection("Data Source=10.212.78.239;Initial Catalog=SBX_KR_11_1;User Id=SBX_KR_11_1;Password=SBX_KR_11_1");
            SqlCommand cmd = new SqlCommand(ProcedureName, conn);
            cmd.CommandTimeout = 60;
            cmd.CommandType = CommandType.StoredProcedure;
            if (values != null && values.Length > 0)
                cmd.Parameters.AddRange(values);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                DataSet ds = new DataSet();
                adapter.Fill(ds);
    
            }
                finally // NO CATCH ON PURPOSE, want the error to escelate
            {
                conn.Close();
                conn.Dispose();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn;
            conn = new SqlConnection("Data Source=10.212.78.293;Initial Catalog=SBX_KR_11_1;User Id=SBX_KR_11_1;Password=SBX_KR_11_1");
            //conn.Open();
            ExecuteNonQueryStoredProcedure("Get_Progress_Users",
                new SqlParameter("@StartDate", StartDate.Text),
                new SqlParameter("@EndDate", EndDate.Text)
                );
            GridView1.DataSource = ds;
            GridView1.DataBind();
    
            //conn.Close();
        }
    }



    Friday, May 25, 2012 5:50 PM

Answers

  • User28563431 posted
     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;
    public partial class _Default : System.Web.UI.Page
    {    
    
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            public static DataSet ExecuteNonQueryStoredProcedure(string ProcedureName, params SqlParameter[] values)
            {
                DataSet ds = new DataSet();
                SqlConnection conn;
                conn = new SqlConnection("Data Source=10.212.78.239;Initial Catalog=SBX_KR_11_1;User Id=SBX_KR_11_1;Password=SBX_KR_11_1");
                SqlCommand cmd = new SqlCommand(ProcedureName, conn);
                cmd.CommandTimeout = 60;
                cmd.CommandType = CommandType.StoredProcedure;
                if (values != null && values.Length > 0)
                    cmd.Parameters.AddRange(values);
                try
                {
                    conn.Open();                
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = cmd;                
                    adapter.Fill(ds);
    
                }
                finally // NO CATCH ON PURPOSE, want the error to escelate
                {
                    conn.Close();
                    conn.Dispose();
                }
    
                return ds;
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
              DataSet ds =   ExecuteNonQueryStoredProcedure("Get_Progress_Users",
                    new SqlParameter("@StartDate", StartDate.Text),
                    new SqlParameter("@EndDate", EndDate.Text)
                    );
              if (ds != null && ds.Tables.Count > 0)
              {
                  GridView1.DataSource = ds.Tables[0];
                  GridView1.DataBind();
              }
              else
              {
                  //Dataset is null
              }            
            }
        }

    Please let me know in case of any issue.

    Please make sure about connection string,stored procudure and number of parameters that stored procedure required

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 25, 2012 6:04 PM

All replies

  • User28563431 posted
     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;
    public partial class _Default : System.Web.UI.Page
    {    
    
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            public static DataSet ExecuteNonQueryStoredProcedure(string ProcedureName, params SqlParameter[] values)
            {
                DataSet ds = new DataSet();
                SqlConnection conn;
                conn = new SqlConnection("Data Source=10.212.78.239;Initial Catalog=SBX_KR_11_1;User Id=SBX_KR_11_1;Password=SBX_KR_11_1");
                SqlCommand cmd = new SqlCommand(ProcedureName, conn);
                cmd.CommandTimeout = 60;
                cmd.CommandType = CommandType.StoredProcedure;
                if (values != null && values.Length > 0)
                    cmd.Parameters.AddRange(values);
                try
                {
                    conn.Open();                
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = cmd;                
                    adapter.Fill(ds);
    
                }
                finally // NO CATCH ON PURPOSE, want the error to escelate
                {
                    conn.Close();
                    conn.Dispose();
                }
    
                return ds;
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
              DataSet ds =   ExecuteNonQueryStoredProcedure("Get_Progress_Users",
                    new SqlParameter("@StartDate", StartDate.Text),
                    new SqlParameter("@EndDate", EndDate.Text)
                    );
              if (ds != null && ds.Tables.Count > 0)
              {
                  GridView1.DataSource = ds.Tables[0];
                  GridView1.DataBind();
              }
              else
              {
                  //Dataset is null
              }            
            }
        }

    Please let me know in case of any issue.

    Please make sure about connection string,stored procudure and number of parameters that stored procedure required

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 25, 2012 6:04 PM
  • User3866881 posted

    Hello:)

    Where's the problem?Plz remove all try……finally and debug your app by pressing F5 to debug to see where the error exception is thrown out?

    Reguards!

    Saturday, May 26, 2012 8:49 PM