Need help desperately on ASP.Net

Discussion in 'Software' started by Olley, Apr 28, 2005.

  1. Olley

    Olley Sergeant

    hello all!

    i have to make this ecommerce webs site in asp.net for uni and am having major problems...
    this is the code that is making the same error no matter what i do:

    void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    addToOrder();
    }
    }

    public void addToOrder()
    {
    //Build connection object //
    conn = new OleDbConnection() ;
    string connStr;
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
    connStr += "Data Source= " + Server.MapPath("call.mdb");
    conn.ConnectionString = connStr;
    queryString4 = "insert into tOrder(pcode, qty, ordernbr) select * from tCat where ordernbr = ";
    //queryString4 += Convert.ToString(Session["ID"]);
    //queryString4 += txt1.Text;
    //queryString4 += " and ordernbr = ";
    queryString4 += Convert.ToString(Session["sordernbr"]);
    //queryString4 += ")";

    OleDbCommand dbCommand4 = new OleDbCommand();
    dbCommand4.CommandText = queryString4;
    dbCommand4.Connection = conn;
    conn.Open();
    dbCommand4.ExecuteNonQuery();
    conn.Close();
    } // end of addToCart
    }


    this is the error i get in Web Matrix:
    Compiler Error Message: BC30188: Declaration expected.

    Line 4: void Page_Load(object sender, EventArgs e)


    im a noob at asp.net and have missed too many lectures.. i would really appreciate anyones help!
     
  2. Olley

    Olley Sergeant

    ok, never mind my previous post, i think i got that to work.. now i hava different problem!
    ive managed to connect my html file to the .cs file but there seems to be a problem with the cs file connecting to the DB.

    the error reads: "No value given for one or more required parameters. "

    this is the code im using for this bit:


    public void BindDataGrid()
    {
    //Build connection object to charlottes.mdb//
    OleDbConnection conn = new OleDbConnection() ;
    string connStr;
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
    connStr += "Data Source= " + Server.MapPath("charlottes.mdb");
    conn.ConnectionString = connStr;

    // Build command object //
    string queryString;
    vKey = Convert.ToInt32(Session["sorderNbr"]);
    queryString = "SELECT Car_Model, Car_Make,Price, qty, ID FROM t_Cars WHERE order_nbr = " + vKey;
    OleDbCommand dbCommand = new OleDbCommand();
    dbCommand.CommandText = queryString;
    dbCommand.Connection = conn;


    // Open the connection //
    conn.Open();

    // Build and fill the dataReader //
    OleDbDataReader dataReader = dbCommand.ExecuteReader(); <<<<---- This is where the error is

    // Bind the data to the datagrid
    empList.DataSource=dataReader;
    empList.DataBind();

    conn.Close();

    }


    i really hope someone can help me with this cause ive been tryin to fix this crap for 2 days now... and i cant afford to lose all my hair because of it!!
     
  3. Kodo

    Kodo SNATCHSQUATCH

    console.write out all your variables in the sql query..make sure they are being populated as expected.
     
  4. Olley

    Olley Sergeant

    im new to this and dont really understand what u mean..
    i think my main problem is with this line:

    queryString = "SELECT Car_Model, Car_Make,Price, qty, ID FROM t_Cars WHERE order_nbr = " + vKey;

    i know that it means: Select Car_Model, Car_Make,Price, qty, ID from the t_Cars table.
    But i dont exactly understand what this does: WHERE order_nbr = " + vKey;

    i know these may seem like dumb questions... im still learnin
    thanks for ur prompt reply Kodo
     
  5. Kodo

    Kodo SNATCHSQUATCH

    WHERE order_nbr = " + vKey; is basically a filter on your query to select only those records where order_nbr EQUALS the value that is assigned to vKey. If vKey is null or empty then your query will fail because it is expecting a parameter that has no value.

    So, you need to make sure that vKey is populated. You have to determine how it's being populated because that is something I cannot do for you without seeing the application or form or whatever is driving this event.

    You can put CONSOLE.WRITELINE vKey and look at your IMMEDIATE WINDOW on the bottom to view the value of vKey or you can put a watch on the lines of code that correspond to your SQL statement and mouse over the vKey variable and it will tooltip you with the value of vKey. (put a watch on the line by clicking on the grey bar to the left of the line , a red dot should appear. The dot should be inline with the line you want to watch).

    additionally, you should close out to connection to your reader after you execute, like this.
    OleDbDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.closeconnection);
     
  6. Olley

    Olley Sergeant

    thanks man! Im gonna try it right now
     
  7. Olley

    Olley Sergeant

    maybe im just too stupid to get this right. I still cant figure it out. Im posting my code so in case someone has done this before maybe s/he can spot the error.

    here is my .cs file:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.OleDb;
    using System.Web.Mail;
    //using System.DateTime;

    namespace Orders
    {

    public class Cars : Page
    {

    public int vKey;
    public int vOrderNbr;
    public int vOrderNbrNew;
    public string queryString4;
    public string queryString5;
    public TextBox make1;
    public TextBox model1;
    public TextBox Price1;
    public TextBox txt1;
    public DataList dl1;
    public OleDbConnection conn;
    public DataGrid empList;
    public string MyMessage;

    public void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    BindDataGrid();
    addToOrder();
    addToOrderDate();
    }
    }

    public void addToOrder()
    {

    //Build connection object to access charlottes.mdb//
    conn = new OleDbConnection() ;
    string connStr;
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
    //The provider is software that supports the interface with a particular DBMS
    connStr += "Data Source= " + Server.MapPath("charlottes.mdb");
    conn.ConnectionString = connStr;

    queryString4 = "insert into t_Order (pcode, qty, order_nbr) select * FROM t_Cart WHERE order_nbr = ";
    //queryString4 += Convert.ToString(Session["ID"]);
    //queryString4 += txt1.Text;
    //queryString4 += " and ordernbr = ";
    queryString4 += Convert.ToString(Session["sordernbr"]);
    //queryString4 += ")";

    OleDbCommand dbCommand4 = new OleDbCommand();
    dbCommand4.CommandText = queryString4;
    dbCommand4.Connection = conn;
    conn.Open();
    dbCommand4.ExecuteNonQuery();

    conn.Close();


    } // end of addToOrder

    public void BindDataGrid()
    {
    //Build connection object to charlottes.mdb//
    OleDbConnection conn = new OleDbConnection() ;
    string connStr;
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
    connStr += "Data Source= " + Server.MapPath("charlottes.mdb");
    conn.ConnectionString = connStr;

    // Build command object //
    string queryString;
    vKey = Convert.ToInt32(Session["sorderNbr"]);
    queryString = "SELECT Car_Model, Car_Make,Price, qty, ID FROM t_Cars WHERE order_nbr = " + CONSOLE.WRITELINE vKey;
    OleDbCommand dbCommand = new OleDbCommand();
    dbCommand.CommandText = queryString;
    dbCommand.Connection = conn;


    // Open the connection //
    conn.Open();

    // Build and fill the dataReader //
    OleDbDataReader dataReader = dbCommand.ExecuteReader();

    // Bind the data to the datagrid
    // Data binding provides the loop automatically
    empList.DataSource=dataReader;
    empList.DataBind();

    conn.Close();

    }

    public void addToOrderDate()
    {

    string hcemail;
    //int ordernbr;

    hcemail="redmelo@hotmail.com";
    //ordernbr=12;

    //Build connection object to charlottes.mdb//
    conn = new OleDbConnection() ;
    string connStr;
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
    connStr += "Data Source= " + Server.MapPath("charlottes.mdb");
    conn.ConnectionString = connStr;

    queryString5 = "insert into OrderDate (Order_nbr,Email,Odate) VALUES (";
    //queryString4 += Convert.ToString(Session["ID"]);
    //queryString4 += txt1.Text;
    //queryString4 += " and ordernbr = ";
    queryString5 += Convert.ToString(Session["sordernbr"]) + ", '";
    queryString5 += hcemail + "', '";
    queryString5 += DateTime.Now + "')";
    //queryString4 += ")";

    OleDbCommand dbCommand5 = new OleDbCommand();
    dbCommand5.CommandText = queryString5;
    dbCommand5.Connection = conn;
    conn.Open();
    dbCommand5.ExecuteNonQuery();

    conn.Close();


    } // end of addToCart

    public void SendMail(object sender, EventArgs e)
    {

    MailMessage MyMessage = new MailMessage();

    MyMessage.To = "redmelo@yahoo.com";
    MyMessage.From = "op6@bton.ac.uk";
    MyMessage.Subject ="Order Confermation Service";
    MyMessage.BodyFormat = MailFormat.Text;

    MyMessage.Body = "Your Order Has Been Proccesed";

    SmtpMail.SmtpServer = "mailhost.bton.ac.uk";

    SmtpMail.Send(MyMessage);
    Response.Redirect("buypro.aspx");

    }

    }
    }
    and this is my html file:

    <%@ Page Debug="true" Inherits="Orders.Cars" Src="NewFile.cs" Language="C#" %>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">
    <div align="center"><asp:Label id="lb1" runat="server" font-size="30pt" text="View Cart" font-bold="True">These
    are Your Order Details:</asp:Label>
    <br />
    &nbsp;
    </div>
    <div align="center">
    <asp:datagrid id="empList" runat="server" AutogenerateColumns="False" BorderWidth="5" CellPadding="5" Font-Size="25" BorderColor="Black" BorderStyle="Solid" BackColor="white">
    <AlternatingItemStyle forecolor="Black"></AlternatingItemStyle>
    <ItemStyle forecolor="olive"></ItemStyle>
    <HeaderStyle forecolor="Firebrick"></HeaderStyle>
    <Columns>
    <asp:BoundColumn DataField="Car_Make" HeaderText="Car Make"></asp:BoundColumn>
    <asp:BoundColumn DataField="Car_Model" HeaderText="Car Model"></asp:BoundColumn>
    <asp:BoundColumn DataField="Qty" HeaderText="Quantity"></asp:BoundColumn>
    <asp:BoundColumn DataField="Price" HeaderText="Price"></asp:BoundColumn>
    </Columns>
    </asp:datagrid>
    <br />
    <br />
    </div>
    <div align="center"><asp:Label id="Label2" runat="server" font-size="20pt" forecolor="red">Please
    review the order to ensure customer satisfaction!</asp:Label>
    <div align="center"><asp:Label id="Label3" runat="server" font-size="15pt" forecolor="black">To
    continue click on Order Now. </asp:Label>
    <div align="center">
    <br />
    <asp:Button id="Button1" onclick="SendMail" runat="server" Width="100px" Height="30px" Text="Order Now"></asp:Button>
    <div>
    </div>
    <div>
    </div>
    </div>
    </div>
    </div>
    </form>
    </body>
    </html>
     
  8. Olley

    Olley Sergeant

    actually i found the problem... i now need to find out how i can Hard Code the Order_Nbr, does anyone know this?
     
  9. Kodo

    Kodo SNATCHSQUATCH

    if the order number is dynamic, then it's not possible or recommended to hardcode the number into the page. Your application will not work as intended. Explain why you want to do this and how/what you discovered your problem?
     
  10. Olley

    Olley Sergeant

    thing is that i only have to do one part of the ecommerce site... there are 4 other people who have to work on their individual part of the website and in the end we can (for extra marks) combine our work to make up a working ecommerce site.. then my part would get the data from the BuyIt part and could save it to the DB. But since its not connected there is no data passed on to it, hence the error. So i was told that i could "simulate" the BuyIt process by hardcodeing it, therefore testing my code fully
     
  11. Kodo

    Kodo SNATCHSQUATCH

    ok , well I assume you mean here

    queryString4 = "insert into t_Order (pcode, qty, order_nbr) select * FROM t_Cart WHERE order_nbr = ";

    just put a number after the = sign
     
  12. Olley

    Olley Sergeant

    i think i got it sorted out, thanks a lot for taking the time and helping me out! i appreciate it....
    btw, how did u manage to make so many posts (22,652)? it must be the record for this site
     
  13. Kodo

    Kodo SNATCHSQUATCH

    No problem.. C# isn't my lang. I do VB.NET but it's almost the same so I was able to muddle through it.. as for posts.. well, I used to be a forum addict. :)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds