G
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();
}
}
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();
}
}