Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 19th, 2003, 10:09 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Portsmouth, Hampshire, United Kingdom.
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default Inserting records into a databae

Guys I am trying to use this to insert records into a database but I am getting this error: -

 OleDbCommand dbComm = new OleDbCommand(strSql, objConnection);
Line 60: dbComm.Parameters.Add("@CategoryName", OleDbType.NVarChar, 15);
Line 61: dbComm.Parameters.Add("@Description", OleDbType.NText);
Line 62:

This is my code,

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>

<html>
  <head>
    <title>validating a text field</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <table id="Table1"
             style="Z-INEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"
             cellspacing="0" cellpadding="0" width="300" border="0">
        <tr>
          <td style="WIDTH: 115px">
            <asp:Label id="Label1" runat="server">Category Name</asp:Label>
          </td>
          <td style="WIDTH: 115px">
            <asp:TextBox id="txtCategoryName" runat="server" width="193"/>
          </td>
        </tr>
        <tr>
          <td style="WIDTH: 115pix">
            <asp:Label id="Label2" runat="server">Description</asp:Label>
          </td>
          <td style="WIDTH: 115px">
            <asp:TextBox id="txtDescription" runat="server" width="193"/>
          </td>
        </tr>
        <tr>
          <td style="WIDTH: 115px" colSpan="2">
            <asp:Button id="btnInsert" runat="server" OnClick="btnInsert_Click" width="298" text="INSERT!" />
          </td>
        </tr>
      </table>
      <asp:RequiredFieldValidator id="rfvCategoryName" runat="server"
           style="Z-INDEX: 102; LEFT: 316px; POSITION: absolute; TOP: 14px"
           ErrorMessage="Please insert the new category name"
           ControlToValidate="txtCategoryName" />
    </form>
  </body>
</html>

<script language = "c#" runat="server">

OleDbConnection objConnection;

private void Page_Load(object sender, System.EventArgs e)
{
  String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source=c:\\test\\Northwind.mdb";
  objConnection = new OleDbConnection(strConnection);
}

private void btnInsert_Click(object sender, System.EventArgs e)
{
  if(Page.IsValid)
  {
    String strSql = "INSERT INTO Categories (CatergoryName, Description) VALUES (@CategoryName, @Description); SELECT @@IDENTY AS 'Identity'";

    OleDbCommand dbComm = new OleDbCommand(strSql, objConnection);
    dbComm.Parameters.Add("@CategoryName", OleDbType.NVarChar, 15);
    dbComm.Parameters.Add("@Description", OleDbType.NText);

    int iID;

    try
    {
      objConnection.Open();
      iID= Convert.ToInt32(dbComm.ExecuteScalar());
    }
    catch (Exception ex)
    {
      Response.Write(ex.Message);
      Response.End();
    }
    finally
    {
      if (objConnection.State == ConnectionState.Open);
      objConnection.Close();
    }

    Response.Write("The ID of the new record is: " +iID.ToString());
  }
}
</script>


    dbComm.Parameters["@CategoryName"].Value = txtCategoryName.Text;

Any ideas

Ads

__________________
Adz - Learning The J2EE Ways.
  #2 (permalink)  
Old October 19th, 2003, 10:47 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You are using SQL Server datatypes, instead of OleDb datatypes. NVarChar and NText are used in a Sql Connection.

Take a look at this page to find out the datatypes you need to pass.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicate records while inserting the data ebindia0041 .NET Framework 1.x 0 May 23rd, 2007 07:15 PM
Inserting Records Question? ersp ADO.NET 1 May 12th, 2004 09:55 AM
Inserting Returned Records hugh@kmcnetwork.com SQL Language 2 November 4th, 2003 01:05 PM



All times are GMT -4. The time now is 12:05 AM.


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