We help IT Professionals succeed at work.

need helpwith a form

Tagom
Tagom used Ask the Experts™
on
I have been asked to create a small form that can be used by our data entry clients. This should be in asp.net -
The form has 4 columns
name, address, delivered (Boolean ), date
I already have the connection to the database done and have been able to pull information but have not been successful at the form - in all honesty - cant even figure out how to begin other than the connections and retrieval.

Could someone please help me get started - This is a volunteer organization helping pregnant teens.

Thank you in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Will you be calling stored procedures to insert the data into the database?

Author

Commented:
If that is the easiest way - the database is access (which I am not really familiar with writing stored procedures in - I usually work in mssql
This is a new and challenging project for me - however it is for a good cause :)
Since you're using MS Access, I don't think you can create stored procedures - I never have anyway. Here is some .Net C# code that should connect to the DB and insert data:

protected void btnSubmit_Click(object sender, EventArgs e)
{
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Server.MapPath("~\\App_Data\\teens.mdb'") + "; Persist Security Info=True");
        try
        {
            myConnection.Open();
            string sqlCmd = "INSERT INTO teens (name, address, delivered, date) VALUES ('Lisa', '123 Somewhere', 1, '08/07/2011')";
            
            OleDbCommand insertCmd = new OleDbCommand(sqlCmd, myConnection);
            insertCmd.ExecuteNonQuery();
            myConnection.Close();            
        }
} 

Open in new window

Author

Commented:
Thank  you very much bloodtrain - I think I did not explain well enough -
That part I have - what I am having trouble figuring out is how to create a form accessible via the website that will allow a teen to register with us....
So I need to write the webbase form that will map the input boxes to the correct variable which will be stored in the database -
I know that in the part of your code it I would have to replace 'Lisa' with @name - this is the part I am stuck with! and then to make sure it submits and inserts.

Ah I see...  below is a basic form:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    
    <p>Name<br />
    <asp:TextBox ID="tb_name" runat="server"></asp:TextBox></p>
    
    <p>Address<br />
    <asp:TextBox ID="tb_address" runat="server"></asp:TextBox></p>
    
    <p>Delivered<br />
    <asp:DropDownList ID="dd_delivered" runat="server">
        <asp:ListItem Value="0">No</asp:ListItem>
        <asp:ListItem Value="1">Yes</asp:ListItem>
    </asp:DropDownList></p>
    
    <p>Date<br />
    <asp:TextBox ID="tb_date" runat="server"></asp:TextBox></p>
    
    <p>
        <asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click" />
    </p>
        
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


Below is the code behind. I don't have MS access installed so I can't really test this but this looks good to me. Once you get the core working, you can then add textbox validation.

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Server.MapPath("~\\App_Data\\teens.mdb'") + "; Persist Security Info=True");
        try
        {
            myConnection.Open();
            string sqlCmd = "INSERT INTO teens (name, address, delivered, date) VALUES ('Lisa', '123 Somewhere', 1, '08/07/2011')";

            OleDbCommand insertCmd = new OleDbCommand(sqlCmd, myConnection);
            insertCmd.ExecuteNonQuery();
            myConnection.Close();
        }
        catch (Exception ex)
        {
            // Handle the error
            // Either log it in a file or email yourself (ex.toString())
        }

    } 

}

Open in new window


Author

Commented:
Thank you so much! I will test it and let you know if it blows up!

Author

Commented:
I must apologize for taking so long to get back to this project!
I used the code mentioned above - however I am getting an error
ERROR: CS1010: Newline in constant
CODE:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       		
		OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Server.MapPath("Data Source = \\mydomain\\outreachStaff\\home\\Southside\\database\\Teens.mdb"") + "; Persist Security Info=True");
        try
        {
            myConnection.Open();
            string sqlCmd = "INSERT INTO Teens (name, address, delivered, date) VALUES ('Lisa', '123 Somewhere', 1, '08/07/2011')";

            OleDbCommand insertCmd = new OleDbCommand(sqlCmd, myConnection);
            insertCmd.ExecuteNonQuery();
            myConnection.Close();
        }
        catch (Exception ex)
        {
            // Handle the error
			
            // Either log it in a file or email yourself (ex.toString())
        }

    } 

}

Open in new window

Author

Commented:
could not get code to work due to connectivity issues - however code did compile.