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