 |
| 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
|
|
|
|

March 16th, 2005, 10:44 AM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

March 16th, 2005, 04:58 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Are you receiving an error message?
|
|

March 17th, 2005, 06:22 AM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes:
Exception Details: System.InvalidCastException: Object must implement IConvertible.
Pointing to
myCmd.ExecuteNonQuery();
|
|

March 17th, 2005, 09:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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.
|
|

March 17th, 2005, 09:20 AM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I entered 30/05/05 in the text box and the value after using Convert.ToDateTime is 30/05/2005/ 00:00:00.
|
|

March 17th, 2005, 01:53 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
What settings do you have for Dates? American dates are Month/Day/Year.
|
|

March 18th, 2005, 06:30 AM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
 |