Professional Web Applications Themes
  1. #1

    Default 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_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.P(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    lblTotaal.DataBind();
    }
    }
    NoSpam Guest

  2. #2

    Default 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_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.P(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    lblTotaal.DataBind();
    }
    }

    "NoSpam" <no@spam.com> wrote in message news:t2mpiv4o1p88o26lq8o9fj296pmspo7noc@4ax.com...
    > 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.P(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    > lblTotaal.DataBind();
    > }
    > }
    carlos medina Guest

  3. #3

    Default 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 [url]http://msdn.microsoft.com/library/en-us/dnbda/html/daag.asp?frame=true[/url]

    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! - [url]www.microsoft.com/security[/url]
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    !From: NoSpam <no@spam.com>
    !Newsgroups: microsoft.public.dotnet.framework.aspnet
    !Subject: C# delete code
    !Message-ID: <t2mpiv4o1p88o26lq8o9fj296pmspo7noc@4ax.com>
    !X-Newsreader: Forte Agent 1.93/32.576 English (American)
    !MIME-Version: 1.0
    !Content-Type: text/plain; cht=us-ascii
    !Content-Transfer-Encoding: 7bit
    !Lines: 62
    !X-Complaints-To: [email]abuse@easynews.com[/email]
    !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!newsfeed 00.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!n ewsfeed1.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.DataGridCommandEventArg s 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.P(GetTotaalFromDatabase().Tables["Totaal"].Rows[0][0].ToString()).ToString("F2");
    ! lblTotaal.DataBind();
    ! }
    !}
    !


    Yan-Hong Huang[MSFT] Guest

  4. #4

    Thumbs down Re: C# Insert Code

    Hi there,

    Im doing this simple application, in the insert button code it seams to be good, but when i run i doesnt insert into the database, below in the code please any help:

    protected void InserirButton_Click(object sender, EventArgs e)
    {
    SqlConnection conn = null;
    string connectionString = ConfigurationManager.ConnectionStrings["CRUD"].ConnectionString;
    using (SqlCommand comm = new SqlCommand("InsertDadosPessoais") { CommandType = System.Data.CommandType.StoredProcedure })
    comm.Parameters.Add(new SqlParameter("@Nome nvarchar", NomeTextBox ));




    try
    {
    conn = new SqlConnection("Server=(localhost);DataBase=CRUD");
    conn.Open();
    //comm.ExecuteNonQuery();
    //comm.Parameters[" "].Value;
    }
    catch
    {
    //dbErrorLabel.Text = "Erro ao adicionar visitante!<br />";z
    }
    finally
    {
    conn.Close();
    }
    }

    P.S: I'm new to programming...
    Unregistered Guest

Similar Threads

  1. Delete a DSN (SQL) through api / code
    By Richard Groen in forum Coldfusion Server Administration
    Replies: 0
    Last Post: November 25th, 12:30 PM
  2. Delete form - Post data to a table and delete uponsubmit.
    By FusionRed in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: July 28th, 01:27 PM
  3. Delete button to delete 3D model
    By mindylim in forum Macromedia Director 3D
    Replies: 1
    Last Post: February 18th, 10:47 AM
  4. Delete key doesn't delete when datagrid is bound to a disconnected table
    By Fred Zolar in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: April 1st, 07:47 AM
  5. Help in my VB Code .. to Delete
    By Sean in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: July 9th, 08:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139