C# delete code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NoSpam

    #1

    C# delete code

    Hi,

    I am working with C# and ASP.NET with code behind and a SQL Server.
    I'm making an e-shop. When clients see what they have in their basket,
    I added a function DELETE to delete a line. It took me hours to get it
    working in both the dataset and the database itself. It works now, but
    the code looks so ugly to me. Can someone tell me what I use too much,
    and what could be changed? I am using a temporary table to store the
    basket with an ID, a session id.

    Thanks!

    private void DataGrid1_Delet eCommand(object source,
    System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    String strConnection =
    ConfigurationSe ttings.AppSetti ngs["xyz"];
    SqlConnection objConnection = new
    SqlConnection(s trConnection);
    objConnection.O pen();
    String strSQL;
    strSQL="SELECT tblProdPrice.Ph oto, tblProducts.Pro duct,
    tblTempOrder.Nu mber, tblProdPrice.Pr ice, tblTempOrder.ID FROM
    tblTempOrder INNER JOIN tblProducts ON tblTempOrder.Pr oductID =
    tblProducts.Web ID INNER JOIN tblProdPrice ON tblProducts.Web ID =
    tblProdPrice.ID WHERE tblTempOrder.Se sID='" + Session.Session ID + "'
    AND tblProducts.Lan g='" + Session["CkTaal"].ToString() + "'";
    SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
    SqlDataAdapter adapter = new SqlDataAdapter( objCommand);
    DataSet mandje = new DataSet();
    adapter.Fill(ma ndje,"Mandje");

    int row = Convert.ToInt32 (e.Item.ItemInd ex);
    string test =
    mandje.Tables["Mandje"].Rows[row]["ID"].ToString();

    mandje.Tables["Mandje"].Rows[row].Delete();

    strSQL="DELETE FROM tblTempOrder WHERE tblTempOrder.ID = '" +
    test + "'";
    SqlCommand objCommand2 = new SqlCommand(strS QL,
    objConnection);
    SqlDataAdapter adapter2 = new SqlDataAdapter( objCommand2);
    DataSet mandje2 = new DataSet();
    adapter2.Fill(m andje2,"Mandje" );
    objConnection.C lose();

    DataGrid1.DataS ource=mandje;
    DataGrid1.DataB ind();

    if
    (GetTotaalFromD atabase().Table s["Totaal"].Rows[0][0].ToString()=="" )
    {
    lblTotaal.Text= "0";
    }
    else
    {
    lblTotaal.Text =
    Double.Parse(Ge tTotaalFromData base().Tables["Totaal"].Rows[0][0].ToString()).To String("F2");
    lblTotaal.DataB ind();
    }
    }
  • carlos medina

    #2
    Re: C# delete code


    maybe is more easy to develop a store procedure that receive three parameters, SessionID, Session["CkTall"] and rowIndex, and inside it make all operation ( query, select the test value and after delete rows ), after it you can return the "query"........ ........... in this form, in the code I'll put only one statement that query in a SqlDataAdapter that does the connection to db and return the DataSet.......


    below are the changes that I'll do it to the code:

    private void DataGrid1_Delet eCommand(object source,
    System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {


    SqlConnection objConnection = new SqlConnection(C onfigurationSet tings.AppSettin gs["xyz"]);

    objConnection.O pen();
    String strSQL;
    strSQL="SELECT tblProdPrice.Ph oto, tblProducts.Pro duct,
    tblTempOrder.Nu mber, tblProdPrice.Pr ice, tblTempOrder.ID FROM
    tblTempOrder INNER JOIN tblProducts ON tblTempOrder.Pr oductID =
    tblProducts.Web ID INNER JOIN tblProdPrice ON tblProducts.Web ID =
    tblProdPrice.ID WHERE tblTempOrder.Se sID='" + Session.Session ID + "'
    AND tblProducts.Lan g='" + Session["CkTaal"].ToString() + "'";
    SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
    SqlDataAdapter adapter = new SqlDataAdapter( objCommand);
    DataSet mandje = new DataSet();
    adapter.Fill(ma ndje,"Mandje");

    int row = Convert.ToInt32 (e.Item.ItemInd ex);
    string test =
    mandje.Tables["Mandje"].Rows[row]["ID"].ToString();

    mandje.Tables["Mandje"].Rows[row].Delete();

    strSQL="DELETE FROM tblTempOrder WHERE tblTempOrder.ID = '" +
    test + "'";
    SqlCommand objCommand2 = new SqlCommand(strS QL,
    objConnection);

    objCommand2.Exe cuteNonQuery();

    objConnection.C lose();

    DataGrid1.DataS ource=mandje;
    DataGrid1.DataB ind();

    if
    (GetTotaalFromD atabase().Table s["Totaal"].Rows[0][0].ToString()=="" )
    {
    lblTotaal.Text= "0";
    }
    else
    {
    lblTotaal.Text =
    Double.Parse(Ge tTotaalFromData base().Tables["Totaal"].Rows[0][0].ToString()).To String("F2");
    lblTotaal.DataB ind();
    }
    }

    "NoSpam" <no@spam.com> wrote in message news:t2mpiv4o1p 88o26lq8o9fj296 pmspo7noc@4ax.c om...[color=blue]
    > Hi,
    >
    > I am working with C# and ASP.NET with code behind and a SQL Server.
    > I'm making an e-shop. When clients see what they have in their basket,
    > I added a function DELETE to delete a line. It took me hours to get it
    > working in both the dataset and the database itself. It works now, but
    > the code looks so ugly to me. Can someone tell me what I use too much,
    > and what could be changed? I am using a temporary table to store the
    > basket with an ID, a session id.
    >
    > Thanks!
    >
    > private void DataGrid1_Delet eCommand(object source,
    > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    > {
    > String strConnection =
    > ConfigurationSe ttings.AppSetti ngs["xyz"];
    > SqlConnection objConnection = new
    > SqlConnection(s trConnection);
    > String strSQL;
    > strSQL="SELECT tblProdPrice.Ph oto, tblProducts.Pro duct,
    > tblTempOrder.Nu mber, tblProdPrice.Pr ice, tblTempOrder.ID FROM
    > tblTempOrder INNER JOIN tblProducts ON tblTempOrder.Pr oductID =
    > tblProducts.Web ID INNER JOIN tblProdPrice ON tblProducts.Web ID =
    > tblProdPrice.ID WHERE tblTempOrder.Se sID='" + Session.Session ID + "'
    > AND tblProducts.Lan g='" + Session["CkTaal"].ToString() + "'";
    > SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
    > SqlDataAdapter adapter = new SqlDataAdapter( objCommand);
    > DataSet mandje = new DataSet();
    > adapter.Fill(ma ndje,"Mandje");
    >
    > int row = Convert.ToInt32 (e.Item.ItemInd ex);
    > string test =
    > mandje.Tables["Mandje"].Rows[row]["ID"].ToString();
    >
    > mandje.Tables["Mandje"].Rows[row].Delete();
    >
    > strSQL="DELETE FROM tblTempOrder WHERE tblTempOrder.ID = '" +
    > test + "'";
    > SqlCommand objCommand2 = new SqlCommand(strS QL,
    > objConnection);
    > SqlDataAdapter adapter2 = new SqlDataAdapter( objCommand2);
    > DataSet mandje2 = new DataSet();
    > adapter2.Fill(m andje2,"Mandje" );
    > objConnection.C lose();
    >
    > DataGrid1.DataS ource=mandje;
    > DataGrid1.DataB ind();
    >
    > if
    > (GetTotaalFromD atabase().Table s["Totaal"].Rows[0][0].ToString()=="" )
    > {
    > lblTotaal.Text= "0";
    > }
    > else
    > {
    > lblTotaal.Text =
    > Double.Parse(Ge tTotaalFromData base().Tables["Totaal"].Rows[0][0].ToString()).To String("F2");
    > lblTotaal.DataB ind();
    > }
    > }[/color]

    Comment

    • Yan-Hong Huang[MSFT]

      #3
      RE: C# delete code

      Hello,

      There is one good article on data acess in .NET.(".NET Data Access Architecture Guide")

      You could get it at http://msdn.microsoft.com/library/en...asp?frame=true

      This article provides information to help you choose the most appropriate data access approach. It does this by describing
      a wide range of common data access scenarios, providing performance tips, and prescribing best practices. This guide
      also provides answers to frequently asked questions, such as: Where is the best place to store database connection
      strings? How should I implement connection pooling? How should I work with transactions? How should I implement paging
      to allow users to scroll through large numbers of records?

      Hope that helps.

      Best regards,
      Yanhong Huang
      Microsoft Online Partner Support

      Get Secure! - www.microsoft.com/security
      This posting is provided "AS IS" with no warranties, and confers no rights.

      --------------------
      !From: NoSpam <no@spam.com>
      !Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
      !Subject: C# delete code
      !Message-ID: <t2mpiv4o1p88o2 6lq8o9fj296pmsp o7noc@4ax.com>
      !X-Newsreader: Forte Agent 1.93/32.576 English (American)
      !MIME-Version: 1.0
      !Content-Type: text/plain; charset=us-ascii
      !Content-Transfer-Encoding: 7bit
      !Lines: 62
      !X-Complaints-To: abuse@easynews. com
      !Organization: EasyNews, UseNet made Easy!
      !X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint
      properly.
      !Date: Sun, 03 Aug 2003 09:43:40 GMT
      !Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-online.de!newsf eed.icl.net!
      newsfeed.fjserv .net!newshostin g.com!news-xfer1.atl.newsh osting.com!140. 99.99.194.MISMA TCH!newsfeed1.e asynews.com!
      easynews.com!ea synews!easynews-local!news.easy news.com.POSTED !not-for-mail
      !Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1644 07
      !X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
      !
      !Hi,
      !
      !I am working with C# and ASP.NET with code behind and a SQL Server.
      !I'm making an e-shop. When clients see what they have in their basket,
      !I added a function DELETE to delete a line. It took me hours to get it
      !working in both the dataset and the database itself. It works now, but
      !the code looks so ugly to me. Can someone tell me what I use too much,
      !and what could be changed? I am using a temporary table to store the
      !basket with an ID, a session id.
      !
      !Thanks!
      !
      !private void DataGrid1_Delet eCommand(object source,
      !System.Web.UI. WebControls.Dat aGridCommandEve ntArgs e)
      !{
      ! String strConnection =
      !ConfigurationS ettings.AppSett ings["xyz"];
      ! SqlConnection objConnection = new
      !SqlConnection( strConnection);
      ! objConnection.O pen();
      ! String strSQL;
      ! strSQL="SELECT tblProdPrice.Ph oto, tblProducts.Pro duct,
      !tblTempOrder.N umber, tblProdPrice.Pr ice, tblTempOrder.ID FROM
      !tblTempOrder INNER JOIN tblProducts ON tblTempOrder.Pr oductID =
      !tblProducts.We bID INNER JOIN tblProdPrice ON tblProducts.Web ID =
      !tblProdPrice.I D WHERE tblTempOrder.Se sID='" + Session.Session ID + "'
      !AND tblProducts.Lan g='" + Session["CkTaal"].ToString() + "'";
      ! SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
      ! SqlDataAdapter adapter = new SqlDataAdapter( objCommand);
      ! DataSet mandje = new DataSet();
      ! adapter.Fill(ma ndje,"Mandje");
      !
      ! int row = Convert.ToInt32 (e.Item.ItemInd ex);
      ! string test =
      !mandje.Tables["Mandje"].Rows[row]["ID"].ToString();
      !
      ! mandje.Tables["Mandje"].Rows[row].Delete();
      !
      ! strSQL="DELETE FROM tblTempOrder WHERE tblTempOrder.ID = '" +
      !test + "'";
      ! SqlCommand objCommand2 = new SqlCommand(strS QL,
      !objConnection) ;
      ! SqlDataAdapter adapter2 = new SqlDataAdapter( objCommand2);
      ! DataSet mandje2 = new DataSet();
      ! adapter2.Fill(m andje2,"Mandje" );
      ! objConnection.C lose();
      !
      ! DataGrid1.DataS ource=mandje;
      ! DataGrid1.DataB ind();
      !
      ! if
      !(GetTotaalFrom Database().Tabl es["Totaal"].Rows[0][0].ToString()=="" )
      ! {
      ! lblTotaal.Text= "0";
      ! }
      ! else
      ! {
      ! lblTotaal.Text =
      !Double.Parse(G etTotaalFromDat abase().Tables["Totaal"].Rows[0][0].ToString()).To String("F2");
      ! lblTotaal.DataB ind();
      ! }
      !}
      !


      Comment

      Working...