Hi
If the textbox_deli is empty it could just insert the fruit and vegtables in the database, and if there is something in textbox_deli it could insert all things in the database.
It do that, but there is comming and error when its finish.
What am I doing wrong??
Object reference not set to an instance of an object.
Line 140: List<ValgtFrugt> valgteFrugter = (List<ValgtFrugt>)Session["ValgteFrugter"];
Line 141:
Line 142: var dataTable = new DataTable(Session["ValgteFrugter"].ToString());
Line 143:
Line 144:
My backend code:
protected void Button_putikurv_Click(object sender, EventArgs e)
{
int totalvaerdi = 0;
List<ValgtDeli> valgteDeli = new List<ValgtDeli>();
foreach (DataListItem itm in VisDeli.Items)
{
if (itm.ItemType == ListItemType.Item || itm.ItemType == ListItemType.AlternatingItem)
{
TextBox TextBox_deli = itm.FindControl("TextBox_deli") as TextBox;
string DeliNavn = TextBox_deli.Attributes["DeliNavn"];
int deliID = Convert.ToInt32(TextBox_deli.Attributes["ID"]);
if (TextBox_deli.Text != null)
{
if (string.IsNullOrEmpty(TextBox_deli.Text))
{
TextBox_deli.Text = "0";
}
}
int enkeltDeliAntal = int.Parse(TextBox_deli.Text);
totalvaerdi += (enkeltDeliAntal);
if (enkeltDeliAntal > 0)
{
ValgtDeli frugtenDerskalTilfoejes = new ValgtDeli();
frugtenDerskalTilfoejes.ID = deliID;
frugtenDerskalTilfoejes.Navn = DeliNavn;
frugtenDerskalTilfoejes.Antal = enkeltDeliAntal;
valgteDeli.Add(frugtenDerskalTilfoejes);
}
}
if (totalvaerdi > 0)
{
Session["ValgteDeli"] = valgteDeli;
opretOrdre();
}
else
{
opretOrdre();
}
}
}
private void opretOrdre()
{
if (Session["ValgteDeli"] == null )
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO ordre (dato, navn, efternavn, mobil, adresse, mail, postnr, bynavn) VALUES(GETDATE(), @navn, @efternavn, @mobil, @adresse, @mail, @postnr, @bynavn); SELECT SCOPE_IDENTITY()";
cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
cmd.Parameters.Add("@efternavn", SqlDbType.VarChar).Value = TextBox_efternavn.Text;
cmd.Parameters.Add("@mobil", SqlDbType.VarChar).Value = TextBox_mobil.Text;
cmd.Parameters.Add("@adresse", SqlDbType.VarChar).Value = TextBox_adresse.Text;
cmd.Parameters.Add("@mail", SqlDbType.VarChar).Value = TextBox_mail.Text;
cmd.Parameters.Add("@postnr", SqlDbType.VarChar).Value = TextBox_postnr.Text;
cmd.Parameters.Add("@bynavn", SqlDbType.VarChar).Value = TextBox_by.Text;
conn.Open();
object orderId = cmd.ExecuteScalar();
conn.Close();
cmd.Parameters.Add("@fk_ordre", SqlDbType.Int).Value = orderId;
cmd.Parameters.Add("@Antal", SqlDbType.Int).Value = 0;
List<ValgtFrugt> valgteFrugter = (List<ValgtFrugt>)Session["ValgteFrugter"];
var dataTable = new DataTable(Session["ValgteFrugter"].ToString());
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Navn", typeof(string));
dataTable.Columns.Add("Antal", typeof(int));
dataTable.Columns.Add("Vaerdi", typeof(int));
foreach (ValgtFrugt frugt in valgteFrugter)
{
dataTable.Rows.Add(frugt.ID, frugt.Navn, frugt.Antal, frugt.Vaerdi);
}
List<ValgtGroent> valgteGroent = (List<ValgtGroent>)Session["ValgteGroent"];
var dataTable1 = new DataTable(Session["ValgteGroent"].ToString());
dataTable1.Columns.Add("ID", typeof(int));
dataTable1.Columns.Add("Navn", typeof(string));
dataTable1.Columns.Add("Antal", typeof(int));
dataTable1.Columns.Add("Vaerdi", typeof(int));
foreach (ValgtGroent groent in valgteGroent)
{
dataTable1.Rows.Add(groent.ID, groent.Navn, groent.Antal, groent.Vaerdi);
}
DataTable dtAll = new DataTable();
dtAll = dataTable.Copy();
dtAll.Merge(dataTable1, true);
conn.Open();
foreach (DataRow row in dtAll.Rows)
{
cmd.CommandText = @"INSERT INTO ordre_linie
(fk_ordre_id, antal, navn)
VALUES (@fk_ordre, @Antal, @navn)";
cmd.Parameters["@Antal"].Value = row["Antal"];
cmd.Parameters["@navn"].Value = row["Navn"];
cmd.ExecuteNonQuery();
}
conn.Close();
Session.Abandon();
Session.Contents.RemoveAll();
Label_tak.Text = "Tak for henvendelsen, ordren bliver behandlet hurtigst muligt";
}
else
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO ordre (dato, navn, efternavn, mobil, adresse, mail, postnr, bynavn) VALUES(GETDATE(), @navn, @efternavn, @mobil, @adresse, @mail, @postnr, @bynavn); SELECT SCOPE_IDENTITY()";
cmd.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox_navn.Text;
cmd.Parameters.Add("@efternavn", SqlDbType.VarChar).Value = TextBox_efternavn.Text;
cmd.Parameters.Add("@mobil", SqlDbType.VarChar).Value = TextBox_mobil.Text;
cmd.Parameters.Add("@adresse", SqlDbType.VarChar).Value = TextBox_adresse.Text;
cmd.Parameters.Add("@mail", SqlDbType.VarChar).Value = TextBox_mail.Text;
cmd.Parameters.Add("@postnr", SqlDbType.VarChar).Value = TextBox_postnr.Text;
cmd.Parameters.Add("@bynavn", SqlDbType.VarChar).Value = TextBox_by.Text;
conn.Open();
object orderId = cmd.ExecuteScalar();
conn.Close();
cmd.Parameters.Add("@fk_ordre", SqlDbType.Int).Value = orderId;
cmd.Parameters.Add("@Antal", SqlDbType.Int).Value = 0;
List<ValgtFrugt> valgteFrugter = (List<ValgtFrugt>)Session["ValgteFrugter"];
var dataTable = new DataTable(Session["ValgteFrugter"].ToString());
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Navn", typeof(string));
dataTable.Columns.Add("Antal", typeof(int));
dataTable.Columns.Add("Vaerdi", typeof(int));
foreach (ValgtFrugt frugt in valgteFrugter)
{
dataTable.Rows.Add(frugt.ID, frugt.Navn, frugt.Antal, frugt.Vaerdi);
}
List<ValgtGroent> valgteGroent = (List<ValgtGroent>)Session["ValgteGroent"];
var dataTable1 = new DataTable(Session["ValgteGroent"].ToString());
dataTable1.Columns.Add("ID", typeof(int));
dataTable1.Columns.Add("Navn", typeof(string));
dataTable1.Columns.Add("Antal", typeof(int));
dataTable1.Columns.Add("Vaerdi", typeof(int));
foreach (ValgtGroent groent in valgteGroent)
{
dataTable1.Rows.Add(groent.ID, groent.Navn, groent.Antal, groent.Vaerdi);
}
List<ValgtDeli> valgteDeli = (List<ValgtDeli>)Session["ValgteDeli"];
var dataTable2 = new DataTable(Session["ValgteDeli"].ToString());
dataTable2.Columns.Add("ID", typeof(int));
dataTable2.Columns.Add("Navn", typeof(string));
dataTable2.Columns.Add("Antal", typeof(int));
foreach (ValgtDeli delikatesse in valgteDeli)
{
dataTable2.Rows.Add(delikatesse.ID, delikatesse.Navn, delikatesse.Antal);
}
DataTable dtAll = new DataTable();
dtAll = dataTable.Copy();
dtAll.Merge(dataTable1, true);
dtAll.Merge(dataTable2, true);
conn.Open();
foreach (DataRow row in dtAll.Rows)
{
cmd.CommandText = @"INSERT INTO ordre_linie
(fk_ordre_id, antal, navn)
VALUES (@fk_ordre, @Antal, @navn)";
cmd.Parameters["@Antal"].Value = row["Antal"];
cmd.Parameters["@navn"].Value = row["Navn"];
cmd.ExecuteNonQuery();
}
conn.Close();
Session.Abandon();
Session.Contents.RemoveAll();
Label_tak.Text = "Tak for henvendelsen, ordren bliver behandlet hurtigst muligt uden deli";
}
}
/Tina