Forum Moderators: open
I need to store First Name and Last Name of users
which come as user inputs through text boxes in
a web form to an existing data table as a new data row.
I used DataSets. I heard it is bulky. Is there any other
way of achieving it? I have included the database
connection in a method that is called for the click
event of the submit button. Instead, Should I include
it in the Page load event?
I also want to know weather there is a way to give
relative paths for database connection instead of
an absolute one which won't be practical when
I move the stuff to a web server.
Below I have included the complete code even with
HTML. The error I get is "Update requires a valid
InsertCommand when passed DataRow collection
with new rows"
It occurs due to the statement,
objCmd.Update(ds, "contacts")
Please help me to complete the code. I am a
trainee and this is a practice project that
I should submit. I couldn't complete it for
2 weeks. I made forum posts and got MSDN
links. Please don't do that. Just tell me what code
to where I should add. I really appreciate your
help. Here is the code,
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Add(obj as object, e as EventArgs)
dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\users\database\users.mdb")
objConn.Open()
dim objCmd as new OleDbDataAdapter("select * from contacts", objConn)
dim ds as DataSet = new DataSet()
objCmd.Fill(ds, "contacts")
dim dr as DataRow = ds.Tables("contacts").NewRow()
dr(1)=txtFirstName.text
dr(2)=txtLastName.Text
ds.Tables("contacts").Rows.Add(dr)
objCmd.Update(ds, "contacts")
objConn.Close()
end sub
</script>
<html>
<body>
<form runat="server">
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td width="40%" align="right"><strong>First Name</strong></td>
<td width="60%">
<asp:textbox ID="txtFirstName" runat="server" />
</td>
</tr>
<tr>
<td align="right"><strong>Last Name</strong></td>
<td>
<asp:textbox ID="txtLastName" runat="server" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<asp:button ID="btnAdd" runat="server" OnClick="Add" Text="Add" />
</td>
</tr>
</table>
</form>
</body>
</html>
The error says that a valid InsertCommand is needed. From your code I make up that there is no InserCommand defined on the OleDbDataAdapter (as you only specied the select command). Something like objCmd.InserCommand = new OleCommand("Insert etc");
Relax, sit back and take some tea. Really try to understand the errormessage and the context. What I do if I do not understand a certain error message, I look it up in google groups as I'm most of the time never the first on earth seeing that error message.
Good luck,
Ward
If you still want to be a web developer/programmer then I would suggest going down to the local book store and picking up some books for ASP.Net development.