Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 16th, 2005, 10:44 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting data into a SQL Server database.

I am a beginner to ASP.NET, using C#. I have set myself a project to develop a 'mini' applications that does the basic functions, list, search, add, update and delete.

I am having a problem with the add part. I have a stored procedure called AddJob which takes in the parameters from the form.

The code us as follows:-

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

    void btnAdd_Click(object sender, EventArgs e)
     {


         string connectionString = "PROVIDER=SQLOLEDB.1;Data Source=MULTIMEDIA2;Initial Catalog=nkonye_test;Integrated Security=SSPI;";
         // Create new SQL command named sqlInsert

         System.Data.SqlClient.SqlCommand myCmd = new System.Data.SqlClient.SqlCommand("AddJob", connectionString);

        // Set the command type property to StoredProcedure

        myCmd.CommandType = CommandType.StoredProcedure;


        // @Title should have the value inside txtTitle
         myCmd.Parameters.Add ( "@Title", SqlDbType.VarChar ).Value = txtTitle.Text;
         myCmd.Parameters.Add ( "@Description", SqlDbType.Text ).Value = txtDetails.Text;
         myCmd.Parameters.Add ( "@Location", SqlDbType.VarChar ).Value = txtLocation.Text;
         myCmd.Parameters.Add ( "@Start", SqlDbType.DateTime ).Value = Convert.ToDateTime(txtStart.Text);
         myCmd.Parameters.Add ( "@End", SqlDbType.DateTime ).Value = Convert.ToDateTime(txtStart.Text);
         myCmd.Parameters.Add ( "@Type", SqlDbType.VarChar ).Value = Type.SelectedItem;
         myCmd.Parameters.Add ( "@Salary", SqlDbType.VarChar ).Value = txtSalary.Text;

        // Open the connection

        connectionString.Open();

       // Execute the query

       myCmd.ExecuteNonQuery();

       // Close the connection

       connectionString.Close();



         // return to record listing
         Response.Redirect ("list.aspx");
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <table cellpadding="0" width="750" align="center" cellpsacing="0">
            <tbody>
                <tr>
                    <td align="middle" colspan="2">
                        <a href="list.aspx">List</a> | <a href="search.aspx">Search</a> | <a href="index.aspx">Log
                        out</a>
                    </td>
                </tr>
                <tr>
                    <td>
                         </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <h2>Add a new job
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td>
                         </td>
                </tr>
                <tr>
                    <td>
                        Job title:</td>
                    <td>
                        <asp:textbox id="txtTitle" runat="server" size="45px"></asp:textbox>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="txtTitle" ErrorMessage="Job title required"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Location:</td>
                    <td>
                        <asp:textbox id="txtLocation" runat="server" size="20px"></asp:textbox>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="txtLocation" ErrorMessage="Job location required"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Type:</td>
                    <td>
                        <asp:dropdownlist id="Type" runat="server">
                            <asp:listitem id="Permanent">Permanent</asp:listitem>
                            <asp:listitem id="Contract">Contract</asp:listitem>
                        </asp:dropdownlist>
                    </td>
                </tr>
                <tr>
                    <td>
                        Salary:</td>
                    <td>
                        <asp:textbox id="txtSalary" runat="server" size="20px"></asp:textbox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Details:</td>
                    <td>
                        <asp:textbox id="txtDetails" runat="server" TextMode="MultiLine" rows="5" cols="40"></asp:textbox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Start date:</td>
                    <td>
                        <asp:textbox id="txtStart" runat="server" size="20px"></asp:textbox>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" ControlToValidate="txtStart" ErrorMessage="Start date required in dd/mm/yyyy format"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        End date:</td>
                    <td>
                        <asp:textbox id="txtEnd" runat="server" size="20px"></asp:textbox>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" ControlToValidate="txtEnd" ErrorMessage="Expiry date required in dd/mm/yyy format"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:button id="btnAdd" onclick="btnAdd_Click" runat="server" Text="Add"></asp:button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>
 
Old March 16th, 2005, 04:58 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Are you receiving an error message?
 
Old March 17th, 2005, 06:22 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes:

Exception Details: System.InvalidCastException: Object must implement IConvertible.

Pointing to

myCmd.ExecuteNonQuery();
 
Old March 17th, 2005, 09:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Make sure all of the parameters match the columns in your database.
myCmd.Parameters.Add ( "@Title", SqlDbType.VarChar ).Value = txtTitle.Text;
myCmd.Parameters.Add ( "@Description", SqlDbType.Text ).Value = txtDetails.Text;
myCmd.Parameters.Add ( "@Location", SqlDbType.VarChar ).Value = txtLocation.Text;
myCmd.Parameters.Add ( "@Start", SqlDbType.DateTime ).Value = Convert.ToDateTime(txtStart.Text);
myCmd.Parameters.Add ( "@End", SqlDbType.DateTime ).Value = Convert.ToDateTime(txtStart.Text);
myCmd.Parameters.Add ( "@Type", SqlDbType.VarChar ).Value = Type.SelectedItem;
myCmd.Parameters.Add ( "@Salary", SqlDbType.VarChar ).Value = txtSalary.Text;

Or check the Convert.ToDateTime(s) and make sure you are inputting valid dates.

 
Old March 17th, 2005, 09:20 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I entered 30/05/05 in the text box and the value after using Convert.ToDateTime is 30/05/2005/ 00:00:00.
 
Old March 17th, 2005, 01:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

What settings do you have for Dates? American dates are Month/Day/Year.

 
Old March 18th, 2005, 06:30 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured it out anyway. My btnAdd_Click fuction was to pass parameters into another function called AddJobs, which runs the SQL. This then worked.





Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting data into database using a jsp ronnie JSP Basics 4 January 15th, 2015 11:35 AM
Need help with inserting data into access database ITladybug ADO.NET 1 January 1st, 2006 01:50 PM
inserting data into Sql Database CodeMonkeys C# 6 August 31st, 2004 04:21 PM
inserting some data from xml file to sql server ak Classic ASP XML 1 February 25th, 2004 10:29 AM
failure in inserting data into database ckmun81 Access ASP 3 December 21st, 2003 08:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.