Welcome to Dream.In.Code
Become a C# Expert!

Join 150,422 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,097 people online right now. Registration is fast and FREE... Join Now!




Adding records from a textbox (C# Visual Web Developer)

 
Reply to this topicStart new topic

Adding records from a textbox (C# Visual Web Developer)

Kageoni2
21 Mar, 2008 - 03:23 PM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 2

I'm trying to add new records to my database using 4 textboxes, one for each field in my databases table. I've got ID, Firstname, Surname and Course.

But I can't figure out at all how to link whats Inputted into the textboxes to be added as a new record to my database when the "Insert Record" button is pushed.., i've spent nearly 2 hours searching the internet for tutorials and people asking similar questions but I can't find anything that works.

I think the solution might be in this line of code:


InsertCommand="INSERT INTO [Data1] ([ID], [Firstname], [Surname], [Course]) VALUES ()"



I've tried putting the ID's of my textboses in the "VALUES ()" part..

(e.g. VALUES (ID.Text, Firstname.Text, Surname.Text, Course.Text)


But that doesn't seem to work, I need the values to be what has been inputted into my textboxes, but I'm not sure what the correct way to do that is or what I need to write between the brackets.

My textboxes ID's are ID, Firstname, Surname and Course.





Heres a screenshot of the page, to make it easier to understand what I'm talking about:IPB Image
Fullcode for the page is:
CODE

<%@ Page Language="C#" %>

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

<script runat="server">

  



    private void InsertRecord(object source, EventArgs e)
    {
        AccessDataSource1.Insert();
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    
            <asp:TextBox ID="ID" runat="server"></asp:TextBox>
        <asp:TextBox ID="Firstname" runat="server"></asp:TextBox>
        <asp:Button ID="btnInsertRecord" runat="server" Style="z-index: 100; left: 273px;
            position: absolute; top: 102px" Text="Insert Record" OnClick="InsertRecord" Width="117px" />
        <asp:TextBox ID="Surname" runat="server"></asp:TextBox>
        <asp:TextBox ID="Course" runat="server"></asp:TextBox>
    
    
    
    
    
    
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/unidata.mdb"
            DeleteCommand="DELETE FROM [Data1] WHERE [ID] = ?" InsertCommand="INSERT INTO [Data1] ([ID], [Firstname], [Surname], [Course]) VALUES ()"
            SelectCommand="SELECT * FROM [Data1]" UpdateCommand="UPDATE [Data1] SET [Firstname] = ?, [Surname] = ?, [Course] = ? WHERE [ID] = ?">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Firstname" Type="String" />
                <asp:Parameter Name="Surname" Type="String" />
                <asp:Parameter Name="Course" Type="String" />
                <asp:Parameter Name="ID" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="ID" Type="String" />
                <asp:Parameter Name="Firstname" Type="String" />
                <asp:Parameter Name="Surname" Type="String" />
                <asp:Parameter Name="Course" Type="String" />
            </InsertParameters>
        </asp:AccessDataSource>
    
    </div>

    </form>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Adding Records From A Textbox (C# Visual Web Developer)
21 Mar, 2008 - 05:24 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You are on the right track. Do you see how I am concatenating the textboxes with into the SQL command?

SQL
InsertCommand="INSERT INTO [Data1] ([ID], [Firstname], [Surname], [Course]) VALUES ('" & ID.Text & "', '" & FirstName.Text & "', '" & Surname.Text & "', '" & Course.Text & "')"


Since you are inserting string values into the database, don't forget to enclose them in single quotes. If these should not all be string values then you will have to convert it to the correct data type and remove the single quotes inside the command.
User is offlineProfile CardPM
+Quote Post

Kageoni2
RE: Adding Records From A Textbox (C# Visual Web Developer)
22 Mar, 2008 - 11:16 AM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 2

Thanks for your help, I added that into the values section and for some reason I'm getting these errors now:

Error 1 The server tag is not well formed.
Warning 2 Validation (ASP.Net): Attribute 'ID.Text' is not a valid attribute of element 'AccessDataSource'.
Warning 3 Validation (ASP.Net): Attribute 'Firstname.Text' is not a valid attribute of element 'AccessDataSource'.
Warning 4 Validation (ASP.Net): Attribute 'Surname.Text' is not a valid attribute of element 'AccessDataSource'.
Warning 5 Validation (ASP.Net): Attribute 'Course.Text' is not a valid attribute of element 'AccessDataSource'.



Heres what i've got in there now:

CODE
<%@ Page Language="C#" %>

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

<script runat="server">

  



    private void InsertRecord(object source, EventArgs e)
    {
        AccessDataSource1.Insert();
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    
            <asp:TextBox ID="ID" runat="server"></asp:TextBox>
        <asp:TextBox ID="Firstname" runat="server"></asp:TextBox>
        <asp:Button ID="btnInsertRecord" runat="server" Style="z-index: 100; left: 273px;
            position: absolute; top: 102px" Text="Insert Record" OnClick="InsertRecord" Width="117px" />
        <asp:TextBox ID="Surname" runat="server"></asp:TextBox>
        <asp:TextBox ID="Course" runat="server"></asp:TextBox>
    
    
    
    
    
    
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/unidata.mdb"
            DeleteCommand="DELETE FROM [Data1] WHERE [ID] = ?" InsertCommand="INSERT INTO [Data1] ([ID], [Firstname], [Surname], [Course]) VALUES ('" + ID.Text + "'," Firstname.Text + "', " + Surname.Text + "', " + Course.Text + "')"
            SelectCommand="SELECT [ID], [Firstname], [Surname], [Course] FROM [Data1]" UpdateCommand="UPDATE [Data1] SET [Firstname] = ?, [Surname] = ?, [Course] = ? WHERE [ID] = ?">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Firstname" Type="String" />
                <asp:Parameter Name="Surname" Type="String" />
                <asp:Parameter Name="Course" Type="String" />
                <asp:Parameter Name="ID" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="ID" Type="String" />
                <asp:Parameter Name="Firstname" Type="String" />
                <asp:Parameter Name="Surname" Type="String" />
                <asp:Parameter Name="Course" Type="String" />
            </InsertParameters>
        </asp:AccessDataSource>
    
    </div>

    </form>
</body>
</html>



I've added these two screenshots to give a better idea of what i've talking about:

IPB Image
IPB Image



User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Adding Records From A Textbox (C# Visual Web Developer)
22 Mar, 2008 - 04:26 PM
Post #4

D.I.C Addict
Group Icon

Joined: 21 Mar, 2008
Posts: 546



Thanked: 29 times
Dream Kudos: 125
My Contributions
you need single quotes before and after the commas inside the INSERT statement.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month