C# delete code

Discussion in 'ASP .Net' started by NoSpam, Aug 3, 2003.

  1. NoSpam

    NoSpam Guest

    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_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    String strConnection =
    ConfigurationSettings.AppSettings["xyz"];
    SqlConnection objConnection = new
    SqlConnection(strConnection);
    objConnection.Open();
    String strSQL;
    strSQL="SELECT tblProdPrice.Photo, tblProducts.Product,
    tblTempOrder.Number, tblProdPrice.Price, tblTempOrder.ID FROM
    tblTempOrder INNER JOIN tblProducts ON tblTempOrder.ProductID =
    tblProducts.WebID INNER JOIN tblProdPrice ON tblProducts.WebID =
    tblProdPrice.ID WHERE tblTempOrder.SesID='" + Session.SessionID + "'
    AND tblProducts.Lang='" + Session["CkTaal"].ToString() + "'";
    SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
    SqlDataAdapter adapter = new SqlDataAdapter(objCommand);
    DataSet mandje = new DataSet();
    adapter.Fill(mandje,"Mandje");

    int row = Convert.ToInt32(e.Item.ItemIndex);
    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(strSQL,
    objConnection);
    SqlDataAdapter adapter2 = new SqlDataAdapter(objCommand2);
    DataSet mandje2 = new DataSet();
    adapter2.Fill(mandje2,"Mandje");
    objConnection.Close();

    DataGrid1.DataSource=mandje;
    DataGrid1.DataBind();

    if
    (GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()=="")
    {
    lblTotaal.Text="0";
    }
    else
    {
    lblTotaal.Text =
    Double.Parse(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    lblTotaal.DataBind();
    }
    }
     
    NoSpam, Aug 3, 2003
    #1
    1. Advertising

  2. 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_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {


    SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["xyz"]);

    objConnection.Open();
    String strSQL;
    strSQL="SELECT tblProdPrice.Photo, tblProducts.Product,
    tblTempOrder.Number, tblProdPrice.Price, tblTempOrder.ID FROM
    tblTempOrder INNER JOIN tblProducts ON tblTempOrder.ProductID =
    tblProducts.WebID INNER JOIN tblProdPrice ON tblProducts.WebID =
    tblProdPrice.ID WHERE tblTempOrder.SesID='" + Session.SessionID + "'
    AND tblProducts.Lang='" + Session["CkTaal"].ToString() + "'";
    SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
    SqlDataAdapter adapter = new SqlDataAdapter(objCommand);
    DataSet mandje = new DataSet();
    adapter.Fill(mandje,"Mandje");

    int row = Convert.ToInt32(e.Item.ItemIndex);
    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(strSQL,
    objConnection);

    objCommand2.ExecuteNonQuery();

    objConnection.Close();

    DataGrid1.DataSource=mandje;
    DataGrid1.DataBind();

    if
    (GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()=="")
    {
    lblTotaal.Text="0";
    }
    else
    {
    lblTotaal.Text =
    Double.Parse(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    lblTotaal.DataBind();
    }
    }

    "NoSpam" <> wrote in message news:...
    > 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_DeleteCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > String strConnection =
    > ConfigurationSettings.AppSettings["xyz"];
    > SqlConnection objConnection = new
    > SqlConnection(strConnection);
    > String strSQL;
    > strSQL="SELECT tblProdPrice.Photo, tblProducts.Product,
    > tblTempOrder.Number, tblProdPrice.Price, tblTempOrder.ID FROM
    > tblTempOrder INNER JOIN tblProducts ON tblTempOrder.ProductID =
    > tblProducts.WebID INNER JOIN tblProdPrice ON tblProducts.WebID =
    > tblProdPrice.ID WHERE tblTempOrder.SesID='" + Session.SessionID + "'
    > AND tblProducts.Lang='" + Session["CkTaal"].ToString() + "'";
    > SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
    > SqlDataAdapter adapter = new SqlDataAdapter(objCommand);
    > DataSet mandje = new DataSet();
    > adapter.Fill(mandje,"Mandje");
    >
    > int row = Convert.ToInt32(e.Item.ItemIndex);
    > 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(strSQL,
    > objConnection);
    > SqlDataAdapter adapter2 = new SqlDataAdapter(objCommand2);
    > DataSet mandje2 = new DataSet();
    > adapter2.Fill(mandje2,"Mandje");
    > objConnection.Close();
    >
    > DataGrid1.DataSource=mandje;
    > DataGrid1.DataBind();
    >
    > if
    > (GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()=="")
    > {
    > lblTotaal.Text="0";
    > }
    > else
    > {
    > lblTotaal.Text =
    > Double.Parse(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    > lblTotaal.DataBind();
    > }
    > }
     
    carlos medina, Aug 3, 2003
    #2
    1. Advertising

  3. 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-us/dnbda/html/daag.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 <>
    !Newsgroups: microsoft.public.dotnet.framework.aspnet
    !Subject: C# delete code
    !Message-ID: <>
    !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:
    !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.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!newsfeed.icl.net!
    newsfeed.fjserv.net!newshosting.com!news-xfer1.atl.newshosting.com!140.99.99.194.MISMATCH!newsfeed1.easynews.com!
    easynews.com!easynews!easynews-local!news.easynews.com.POSTED!not-for-mail
    !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:164407
    !X-Tomcat-NG: microsoft.public.dotnet.framework.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_DeleteCommand(object source,
    !System.Web.UI.WebControls.DataGridCommandEventArgs e)
    !{
    ! String strConnection =
    !ConfigurationSettings.AppSettings["xyz"];
    ! SqlConnection objConnection = new
    !SqlConnection(strConnection);
    ! objConnection.Open();
    ! String strSQL;
    ! strSQL="SELECT tblProdPrice.Photo, tblProducts.Product,
    !tblTempOrder.Number, tblProdPrice.Price, tblTempOrder.ID FROM
    !tblTempOrder INNER JOIN tblProducts ON tblTempOrder.ProductID =
    !tblProducts.WebID INNER JOIN tblProdPrice ON tblProducts.WebID =
    !tblProdPrice.ID WHERE tblTempOrder.SesID='" + Session.SessionID + "'
    !AND tblProducts.Lang='" + Session["CkTaal"].ToString() + "'";
    ! SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
    ! SqlDataAdapter adapter = new SqlDataAdapter(objCommand);
    ! DataSet mandje = new DataSet();
    ! adapter.Fill(mandje,"Mandje");
    !
    ! int row = Convert.ToInt32(e.Item.ItemIndex);
    ! 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(strSQL,
    !objConnection);
    ! SqlDataAdapter adapter2 = new SqlDataAdapter(objCommand2);
    ! DataSet mandje2 = new DataSet();
    ! adapter2.Fill(mandje2,"Mandje");
    ! objConnection.Close();
    !
    ! DataGrid1.DataSource=mandje;
    ! DataGrid1.DataBind();
    !
    ! if
    !(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()=="")
    ! {
    ! lblTotaal.Text="0";
    ! }
    ! else
    ! {
    ! lblTotaal.Text =
    !Double.Parse(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    ! lblTotaal.DataBind();
    ! }
    !}
    !
     
    Yan-Hong Huang[MSFT], Aug 5, 2003
    #3
  4. NoSpam

    SNK111

    Joined:
    Apr 6, 2010
    Messages:
    6
    How To: Sql & C#

    Check this article: shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html for details on how to connect to SQL Server database from C#.NET database applications as well as Java database applications. It also describes how to pass embedded SQL queries, calling stored procedures, pass parameters etc.

    Here you will find the correct way to do the work you have described, as suggested by Microsoft documentation.
     
    SNK111, Jun 2, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    625
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    394
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    314
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,842
    Mathieu Malaterre
    Aug 17, 2004
  5. Jef Driesen
    Replies:
    1
    Views:
    505
    Gernot Frisch
    Jan 19, 2005
Loading...

Share This Page