Solved

Variable not looping through a datagrid

Posted on 2004-10-25
245 Views
Last Modified: 2010-04-15
Hi

Can anyone help solve this problem, I have just being informed on how to highlight a variable using regex, the problem now is when I enter the variable into the datagrid, all the pagedescriptions are the same even through the rest of the results change correctly.

i.e title and pageurl change for each record, but the description is the same for each. Can anyone help solve this problem.

Below is the code:

void Page_Load(Object Src, EventArgs E)
{
inputString = Request.QueryString["TxtQuery"];
                              string[] words = GetQueryStringToArray(Request.QueryString["TxtQuery"].ToString()) ;
                    Word1 = words[0];
                    Word2 = words[1];
                    Word3 = words[2];
                    Word4 = words[3];
                    Word5 = words[4];
                    Word6 = words[5];
                              HighlightWord = spQueryResults.FieldValue("PageDescription", null);
                    HighlightWord = Regex.Replace(HighlightWord, Word1, "<strong>"+Word1+"</strong>", RegexOptions.IgnoreCase);
                    HighlightWord = Regex.Replace(HighlightWord, Word2, "<strong>"+Word2+"</strong>", RegexOptions.IgnoreCase);
                   
               
          }
     public string[] GetQueryStringToArray(string inputString)
          {
              string[] finalSetOfWords = new string[6];
               char[] separators = {' '};
               string[] words = inputString.Split(separators);
               
               for ( int i=0; i < 6; i++)
               {
                    if(words.Length > i && inputString.Trim().Length > 0)
                    {
                         finalSetOfWords[i] = words[i];
                    }
                    else
                    {
                         finalSetOfWords[i] = "NNN";
                    }
               }
               return finalSetOfWords;
                    
          }

Datadrid details
<Columns>
          <asp:TemplateColumn  runat="server">
<itemtemplate>
             
              <span class="QuCssResultsTitle"><%# spQueryResults.FieldValue("PageTitle", Container) %></span><br>
              <span class="QuCssResultsBody"><%=HighlightWord%></span><br>
            <span class="QuCssResultsURL"><a href="<%# spQueryResults.FieldValue("PageURL", Container) %>"><%# spQueryResults.FieldValue("PageURL", Container) %></a></span></p><br>
        </itemtemplate></asp:TemplateColumn>
      </columns>

Any help would be appreciated
Caz

0
Question by:BeginningWebDesign
    12 Comments
     
    LVL 7

    Expert Comment

    by:sukumar_diya
    Hi pal,
    Please mention the details about the declaration of the variable 'HighlightWord' if possible post full code here. I checked with information given by you its working fine.. The following code is the one i checked..

    protected string HighlightWord="harry new";
                private void Page_Load(object sender, System.EventArgs e)
                {
                      
                      string word1="harry";
                      
                      HighlightWord = Regex.Replace(HighlightWord, word1, "<strong>" + word1 + "</strong>", RegexOptions.IgnoreCase);
                      DataColumn dtcol = new DataColumn("test");
                      DataTable dttbl =new DataTable();
                      dttbl.Columns.Add(dtcol);
                      DataRow dtrow=dttbl.NewRow();
                      dttbl.Rows.Add(dtrow);
                      DataGrid1.DataSource=dttbl;
                      DataGrid1.DataBind();
    }

    Html
    ++++
          <asp:DataGrid id="DataGrid1" runat="server">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
               
               
                <span class="QuCssResultsBody"><%=HighlightWord%></span><br>
             
           
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn></asp:TemplateColumn>
    </Columns>

                      </asp:DataGrid><br>







    0
     

    Author Comment

    by:BeginningWebDesign
    Hi sukumar_diya

    Sorry for delay had to go out and only got back at 10pm tonight, please find full code below.


    void Page_Load(Object Src, EventArgs E)
    {
    inputString = Request.QueryString["TxtQuery"];
                                  string[] words = GetQueryStringToArray(Request.QueryString["TxtQuery"].ToString()) ;
                        Word1 = words[0];
                        Word2 = words[1];
                        Word3 = words[2];
                        Word4 = words[3];
                        Word5 = words[4];
                        Word6 = words[5];
                                  HighlightWord = spQueryResults.FieldValue("PageDescription", null);
                        HighlightWord = Regex.Replace(HighlightWord, Word1, "<strong>"+Word1+"</strong>", RegexOptions.IgnoreCase);
                        HighlightWord = Regex.Replace(HighlightWord, Word2, "<strong>"+Word2+"</strong>", RegexOptions.IgnoreCase);
                                  HighlightWord = Regex.Replace(HighlightWord, Word3, "<strong>"+Word3+"</strong>", RegexOptions.IgnoreCase);
                        HighlightWord = Regex.Replace(HighlightWord, Word4, "<strong>"+Word4+"</strong>", RegexOptions.IgnoreCase);
                                  HighlightWord = Regex.Replace(HighlightWord, Word5, "<strong>"+Word5+"</strong>", RegexOptions.IgnoreCase);
                        HighlightWord = Regex.Replace(HighlightWord, Word6, "<strong>"+Word6+"</strong>", RegexOptions.IgnoreCase);
                        DataColumn dtcol = new DataColumn("test");
                        DataTable dttbl =new DataTable();
                        dttbl.Columns.Add(dtcol);
                        DataRow dtrow=dttbl.NewRow();
                        dttbl.Rows.Add(dtrow);
                        QueryResults.DataSource=dttbl;
                        QueryResults.DataBind();


                   
              }
         public string[] GetQueryStringToArray(string inputString)
              {
                  string[] finalSetOfWords = new string[6];
                   char[] separators = {' '};
                   string[] words = inputString.Split(separators);
                   
                   for ( int i=0; i < 6; i++)
                   {
                        if(words.Length > i && inputString.Trim().Length > 0)
                        {
                             finalSetOfWords[i] = words[i];
                        }
                        else
                        {
                             finalSetOfWords[i] = "NNN";
                        }
                   }
                   return finalSetOfWords;
                        
              }

    </script>

    <form runat="server">
      <asp:DataGrid
      AllowCustomPaging="true"
      AllowPaging="true"
      AllowSorting="false"
      AutoGenerateColumns="false" BorderWidth="0"
      CellPadding="0"
      CellSpacing="0"
      DataSource="<%# spQueryResults.DefaultView %>" id="QueryResults"
      PagerStyle-Mode="NextPrev"
      PagerStyle-NextPageText="Next --&gt;"
      PagerStyle-PrevPageText="&lt;-- Prev."
      PageSize="<%# spQueryResults.PageSize %>"
      runat="server"
      ShowFooter="true"
      ShowHeader="false"
      OnPageIndexChanged="spQueryResults.OnDataGridPageIndexChanged"
      VirtualItemCount="<%# spQueryResults.RecordCount %>" gridline="None"
    >
        <HeaderStyle/>
        <ItemStyle CssClass="QuCssResults"/>
        <AlternatingItemStyle CssClass="QuCssResults"/>
        <FooterStyle/>
        <PagerStyle CssClass="QuCssResultsNav"/>
        <Columns>
              <asp:TemplateColumn  runat="server">
                <itemtemplate>
                  <p><div style="float:right;"><span class="QuCssResultsRele">&nbsp;</span></div><br>
                  <span class="QuCssResultsTitle"><%# spQueryResults.FieldValue("PageTitle", Container) %></span><br>
                  <span class="QuCssResultsBody"><%=HighlightWord%></span><br>
                <span class="QuCssResultsURL"><a href="<%# spQueryResults.FieldValue("PageURL", Container) %>"><%# spQueryResults.FieldValue("PageURL", Container) %></a> </span></p><br>
            </itemtemplate></asp:TemplateColumn>
          </columns>
      </asp:DataGrid>
    </form>

    When I try the code, I receive the following error:

    Compiler Error Message: CS0246: The type or namespace name 'DataColumn' could not be found (are you missing a using directive or an assembly reference?)

    Source Error:

     

    Line 118:                              HighlightWord = Regex.Replace(HighlightWord, Word5, "<strong>"+Word5+"</strong>", RegexOptions.IgnoreCase);
    Line 119:                    HighlightWord = Regex.Replace(HighlightWord, Word6, "<strong>"+Word6+"</strong>", RegexOptions.IgnoreCase);
    Line 120:                    DataColumn dtcol = new DataColumn("test");
    Line 121:                    DataTable dttbl =new DataTable();
    Line 122:                    dttbl.Columns.Add(dtcol);
     

    Source File: C:\***\results.aspx    Line: 120

    Caz
    0
     
    LVL 7

    Expert Comment

    by:sukumar_diya
    Hi pal,
    I have posted the code that is working for me. Please check with that ....

    HTML CODE
    +++++++++

    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="DRDa.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

    <html>
      <head>
        <title>WebForm2</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name=vs_defaultClientScript content="JavaScript">
        <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
       
      </head>
      <body MS_POSITIONING="GridLayout">
          
       <form runat="server" ID="Form1">
      <asp:DataGrid
      AllowCustomPaging="true"
      AllowPaging="true"
      AllowSorting="false"
      AutoGenerateColumns="false" BorderWidth="0"
      CellPadding="0"
      CellSpacing="0"
      id="QueryResults"
      PagerStyle-Mode="NextPrev"
      PagerStyle-NextPageText="Next --&gt;"
      PagerStyle-PrevPageText="&lt;-- Prev."
      runat="server"
      ShowFooter="true"
      ShowHeader="false"
     
      gridline="None"
    >
        <HeaderStyle/>
        <ItemStyle CssClass="QuCssResults"/>
        <AlternatingItemStyle CssClass="QuCssResults"/>
        <FooterStyle/>
        <PagerStyle CssClass="QuCssResultsNav"/>
        <Columns>
             <asp:TemplateColumn  runat="server">
               <itemtemplate>
                <p><div style="float:right;"><span class="QuCssResultsRele">&nbsp;</span></div><br>
                <span class="QuCssResultsTitle">title</span><br>
                <span class="QuCssResultsBody"><%=HighlightWord%></span><br>
              <span class="QuCssResultsURL"><a href="url">url</a> </span></p><br>
           </itemtemplate></asp:TemplateColumn>
         </columns>
      </asp:DataGrid>
    </form>

          
      </body>
    </html>


    Code Behind
    +++++++++

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    namespace DRDa
    {
          /// <summary>
          /// Summary description for WebForm2.
          /// </summary>
          public class WebForm2 : System.Web.UI.Page
          {
                protected System.Web.UI.WebControls.DataGrid QueryResults;
                protected string inputString="";
                protected string Word1="";
                protected string Word2="";
                protected string Word3="";
                protected string Word4="";
                protected string Word5="";
                protected string Word6="";
                protected string HighlightWord="";
                void Page_Load(Object Src, EventArgs E)
                {
                      inputString = "harry john test test2 test3 test4";
                      string[] words = GetQueryStringToArray(inputString) ;
                      Word1 = words[0];
                      Word2 = words[1];
                      Word3 = words[2];
                      Word4 = words[3];
                      Word5 = words[4];
                      Word6 = words[5];
                      HighlightWord = "i am testing with the name harry";
                      HighlightWord = Regex.Replace(HighlightWord, Word1, "<strong>" + Word1 + "</strong>", RegexOptions.IgnoreCase);
                      HighlightWord = Regex.Replace(HighlightWord, Word2, "<strong>" + Word2 + "</strong>", RegexOptions.IgnoreCase);
                      HighlightWord = Regex.Replace(HighlightWord, Word3, "<strong>" + Word3 + "</strong>", RegexOptions.IgnoreCase);
                      HighlightWord = Regex.Replace(HighlightWord, Word4, "<strong>" + Word4 + "</strong>", RegexOptions.IgnoreCase);
                      HighlightWord = Regex.Replace(HighlightWord, Word5, "<strong>" + Word5 + "</strong>", RegexOptions.IgnoreCase);
                      HighlightWord = Regex.Replace(HighlightWord, Word6, "<strong>" + Word6 + "</strong>", RegexOptions.IgnoreCase);
                       
                      DataColumn dtcol = new DataColumn("test");
                      DataTable dttbl =new DataTable();
                      dttbl.Columns.Add(dtcol);
                      DataRow dtrow=dttbl.NewRow();
                      dttbl.Rows.Add(dtrow);
                       
                      QueryResults.DataSource=dttbl;
                      QueryResults.DataBind();


                   
                }
                public string[] GetQueryStringToArray(string inputString)
                {
                      string[] finalSetOfWords = new string[6];
                      char[] separators = {' '};
                      string[] words = inputString.Split(separators);
                   
                      for ( int i=0; i < 6; i++)
                      {
                            if(words.Length > i && inputString.Trim().Length > 0)
                            {
                                  finalSetOfWords[i] = words[i];
                            }
                            else
                            {
                                  finalSetOfWords[i] = "NNN";
                            }
                      }
                      return finalSetOfWords;
                     
                }



                #region Web Form Designer generated code
                override protected void OnInit(EventArgs e)
                {
                      //
                      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                      //
                      InitializeComponent();
                      base.OnInit(e);
                }
                
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {    
                      this.Load += new System.EventHandler(this.Page_Load);
                }
                #endregion
          }
    }


    Cheers
    Sukumar
    0
     

    Author Comment

    by:BeginningWebDesign
    Hi sukumar_diya

    Please bear with me for a while, I use Dreamweaver MX and the problem seems to be with how Dreamweaver databinds the datagrid.

    I do have visual studio but I don't use it because

    1) I cannot work out how to connect to the database using a web config file
    2) I cannot work out how to put the stored procudures on the page and databind them to the datagrid

    I will get back to you shortly once I have worked out how to do the above and then test your code.

    Regards
    Caz
    0
     
    LVL 7

    Expert Comment

    by:sukumar_diya
    Hi pal,

    In wen.config file make your connection string like below

    <appSettings>
    <add key="CONNECTIONSTRING" value="SERVER=#########;DATABASE=######;UID=#####;PWD=#####;Application Name=#####" />
    </appSettings>

    and you can get the connection string in the class as

    string strConnection = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
    SqlConnection objConnection;
    SqlDataAdapter objAdapter;
    DataTable objDataTable=new DataTable();

    objConnection = new SqlConnection(strConnection);
    objAdapter = new SqlDataAdapter("your stored procedure name", objConnection);
    objAdapter.Fill(objDataTable);

    datagrid.datasource= objDataSet .Tables[0];
    datagrid.DataBind();

    objDataTable=null;
    dataAdapterObject.Dispose();
    dataAdapterObject = null;
    connectionObject.Close();
    connectionObject.Dispose();
    connectionObject = null;

    I am using sql server. Hope it helps you...

    cheers
    Sukumar

    0
     

    Author Comment

    by:BeginningWebDesign
    Hi Sukumar

    I'm using sql server too.

    I have the following

    string SearchPhrase;
    SearchPhrase = Request.QueryString["txtSearch"];

    How would I get the value of SearchPhrase and use it to search the stored procedure to return the results. Dreamweaver makes it so easy, that it hard to start and change to visual studio.

    Caz
    0
     
    LVL 7

    Expert Comment

    by:sukumar_diya
    hi Caz,
    I have posted some code here try with that hope it will help you...
    Cheers
    Sukumar

                private void Page_Load(object sender, System.EventArgs e)
                {
                      string SearchPhrase;      
                      if( Request.QueryString["txtSearch"]!=null)
                      {
                            SearchPhrase = Request.QueryString["txtSearch"];
                                                                    DataTable dtTbl = searchfuntion(SearchPhrase,"");
                            datagrid.DataSource= dtTbl;
                            datagrid.DataBind();
                      }
                }

                public DataTable searchfuntion(string param1,string param2)
                {
                      SqlParameter[] colParams = new SqlParameter[2];
                      try
                      {
                            colParams[0] = new SqlParameter("@group_Name",param1);
                            colParams[1] = new SqlParameter("@group_Desc",param2);
                            return ExecuteSQL("usp_anchorage_group_master_insert",colParams);
                      }
                      catch(Exception ex)
                      {
                            throw ex;
                      }
                }      

                /// <summary>
                /// Executes the Stored Procedure with the SqlParameter array object passed
                /// and returns the DataSet
                /// </summary>
                /// <param name="CommandText"></param>
                /// <param name="Params"></param>
                /// <returns></returns>
                private static DataSet ExecuteSQL(String CommandText,SqlParameter[] Params)
                {
                      OpenConnection();
                      SqlCommand cmdObj=new SqlCommand();
                      cmdObj.CommandType=CommandType.StoredProcedure;
                      cmdObj.Connection=Conn;
                      cmdObj.CommandText=CommandText;
                      if (Params.Length>0)
                      {
                            for (int i=0;i<Params.Length;i++)
                            {
                                  cmdObj.Parameters.Add(Params[i]);
                            }
                      }

                      SqlDataAdapter daSP=new SqlDataAdapter(cmdObj);
                      DataSet dsRes=new DataSet();

                      daSP.Fill(dsRes);
                      daSP.Dispose();

                      cmdObj.Dispose();
                      CloseConnection();

                      return dsRes;
                }
                ///
                /// <summary>
                /// Opens the connection to the database.
                /// </summary>
                protected static void OpenConnection()
                {
                      String strConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
                      Conn = new SqlConnection();
                      if (Conn.State==ConnectionState.Open)
                      {
                            Conn.Close();
                      }
                      else
                      {
                            Conn.ConnectionString=strConnectionString;
                            Conn.Open();
                      }
                }
                ///
                /// <summary>
                /// Closes the Connection
                /// </summary>
                protected static void CloseConnection()
                {
                      if (Conn!=null)
                      {
                            if (Conn.State==ConnectionState.Open)
                            {
                                  Conn.Close();
                            }
                            Conn=null;
                      }
                }
    0
     

    Author Comment

    by:BeginningWebDesign
    Hi sukumar_diya

    Tried code with datagrid and receive following error:

    Parser Error Message: Could not load type 'DRDa.WebForm2'.

    Source Error:


    Line 1:  <%@ Page language="c#" Codebehind="WebForm2.cs" AutoEventWireup="false" Inherits="DRDa.WebForm2" %>
    Line 2:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    Line 3:  
     

    Source File: C:\*****\test.aspx    Line: 1

    Caz

    0
     
    LVL 7

    Accepted Solution

    by:
    Hi Caz,
    I am sorry. This is my mistake. i have copied all code i tried in my test project. DRDa is my test project.
    and the webform2 is my form name. So you just copy the code from html tag paste in your form. Don't modify the <%page ...
    in your form. that inherits your code behind to your HTML code.

    in codebehind
    change the namespace name as you want.
    now it is like "namespace DRDa"  

    public class WebForm2 : System.Web.UI.Page
    in this change the webform2 with your form name...

    Sukumar
    0
     

    Author Comment

    by:BeginningWebDesign
    Hi Sukumar

    Still receive error

    I have pasted header code and datagrid code, could you please take a look and change whatever needs changing.

    Caz

    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="DRDa.WebForm2" %>

    <form runat="server">
      <asp:DataGrid
      AllowCustomPaging="true"
      AllowPaging="true"
      AllowSorting="false"
      AutoGenerateColumns="false" BorderWidth="0"
      CellPadding="0"
      CellSpacing="0"
      id="QueryResults"
      PagerStyle-Mode="NextPrev"
      PagerStyle-NextPageText="Next --&gt;"
      PagerStyle-PrevPageText="&lt;-- Prev."
      runat="server"
      ShowFooter="true"
      ShowHeader="false"
     
      gridline="None"
    >
        <HeaderStyle/>
        <ItemStyle CssClass="QuCssResults"/>
        <AlternatingItemStyle CssClass="QuCssResults"/>
        <FooterStyle/>
        <PagerStyle CssClass="QuCssResultsNav"/>
        <Columns>
             <asp:TemplateColumn  runat="server">
               <itemtemplate>
                <p><div style="float:right;"><span class="QuCssResultsRele">&nbsp;</span></div><br>
                <span class="QuCssResultsTitle">title</span><br>
                <span class="QuCssResultsBody"><%=HighlightWord%></span><br>
              <span class="QuCssResultsURL"><a href="url">url</a> </span></p><br>
           </itemtemplate></asp:TemplateColumn>
         </columns>
      </asp:DataGrid>
    </form>


     public class [what goes here] : System.Web.UI.Page
    0
     
    LVL 7

    Expert Comment

    by:sukumar_diya
    Hi caz,
    Please follow this step...

    1) Open a new form in your project and give a name for the form say now it is 'caz.aspx'.
    2) go to the design and html code. paster the following code inside the <form id=...> paste it here </form>
     <asp:DataGrid
      AllowCustomPaging="true"
      AllowPaging="true"
      AllowSorting="false"
      AutoGenerateColumns="false" BorderWidth="0"
      CellPadding="0"
      CellSpacing="0"
      id="QueryResults"
      PagerStyle-Mode="NextPrev"
      PagerStyle-NextPageText="Next --&gt;"
      PagerStyle-PrevPageText="&lt;-- Prev."
      runat="server"
      ShowFooter="true"
      ShowHeader="false"
     
      gridline="None"
    >
        <HeaderStyle/>
        <ItemStyle CssClass="QuCssResults"/>
        <AlternatingItemStyle CssClass="QuCssResults"/>
        <FooterStyle/>
        <PagerStyle CssClass="QuCssResultsNav"/>
        <Columns>
             <asp:TemplateColumn  runat="server">
               <itemtemplate>
                <p><div style="float:right;"><span class="QuCssResultsRele">&nbsp;</span></div><br>
                <span class="QuCssResultsTitle">title</span><br>
                <span class="QuCssResultsBody"><%=HighlightWord%></span><br>
              <span class="QuCssResultsURL"><a href="url">url</a> </span></p><br>
           </itemtemplate></asp:TemplateColumn>
         </columns>
      </asp:DataGrid>


    3) go to the codebehind. Before the namespace u can see 'using' keyword for importing namespace.
    replace all with the following
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;

    4)

    u can see now
    public class caz : System.Web.UI.Page
    {
    }

     in side this find the page_load function. Replace the whole function with the following code

     private void Page_Load(object sender, System.EventArgs e)
              {
                   string SearchPhrase;    
                   if( Request.QueryString["txtSearch"]!=null)
                   {
                        SearchPhrase = Request.QueryString["txtSearch"];
                                                                    DataTable dtTbl = searchfuntion(SearchPhrase,"");
                        datagrid.DataSource= dtTbl;
                        datagrid.DataBind();
                   }
              }

              public DataTable searchfuntion(string param1,string param2)
              {
                   SqlParameter[] colParams = new SqlParameter[2];
                   try
                   {
                        colParams[0] = new SqlParameter("@group_Name",param1);
                        colParams[1] = new SqlParameter("@group_Desc",param2);
                        return ExecuteSQL("usp_anchorage_group_master_insert",colParams);
                   }
                   catch(Exception ex)
                   {
                        throw ex;
                   }
              }    

              /// <summary>
              /// Executes the Stored Procedure with the SqlParameter array object passed
              /// and returns the DataSet
              /// </summary>
              /// <param name="CommandText"></param>
              /// <param name="Params"></param>
              /// <returns></returns>
              private static DataSet ExecuteSQL(String CommandText,SqlParameter[] Params)
              {
                   OpenConnection();
                   SqlCommand cmdObj=new SqlCommand();
                   cmdObj.CommandType=CommandType.StoredProcedure;
                   cmdObj.Connection=Conn;
                   cmdObj.CommandText=CommandText;
                   if (Params.Length>0)
                   {
                        for (int i=0;i<Params.Length;i++)
                        {
                             cmdObj.Parameters.Add(Params[i]);
                        }
                   }

                   SqlDataAdapter daSP=new SqlDataAdapter(cmdObj);
                   DataSet dsRes=new DataSet();

                   daSP.Fill(dsRes);
                   daSP.Dispose();

                   cmdObj.Dispose();
                   CloseConnection();

                   return dsRes;
              }
              ///
              /// <summary>
              /// Opens the connection to the database.
              /// </summary>
              protected static void OpenConnection()
              {
                   String strConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
                   Conn = new SqlConnection();
                   if (Conn.State==ConnectionState.Open)
                   {
                        Conn.Close();
                   }
                   else
                   {
                        Conn.ConnectionString=strConnectionString;
                        Conn.Open();
                   }
              }
              ///
              /// <summary>
              /// Closes the Connection
              /// </summary>
              protected static void CloseConnection()
              {
                   if (Conn!=null)
                   {
                        if (Conn.State==ConnectionState.Open)
                        {
                             Conn.Close();
                        }
                        Conn=null;
                   }
              }

    hope it helps you..
    sukumar
    0
     

    Author Comment

    by:BeginningWebDesign
    Hi sukumar_diya

    I'll close this question down and award the points for all your help.

    Regards
    Caz
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now