E
Elena
I am new to ASP. NET, I can't seem to get my code to work. I am having
problems updating a database through a data grid. below is the error I
receive and below that the code.
Thanks in Advance,
Elena.
Parameter @employee_first_name has no default value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Parameter
@employee_first_name has no default value.
Source Error:
Line 48: cmd1.Parameters.Add("@employee_number", intEmpNo)
Line 49: connTSA.Open()
Line 50: cmd1.ExecuteNonQuery()
Line 51: connTSA.Close()
CODE:
<%@ Page Language="VB" ContentType="text/html" Debug="true" %><%@ Import
Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<Script Runat="Server">
Dim connTSA As OleDbConnection
Dim conStr As String
Dim strSql As String
Sub Page_Load()
conStr = "Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\TSATrackerDB.mdb"
connTSA = New OleDbConnection(conStr)
If Not IsPostBack Then
BindDataGrid()
End If
End Sub
Sub BindDataGrid()
connTSA.Open()
strSql = "Select * from Employee"
Dim cmd As New OleDbCommand(strSql, connTSA)
DataGrid1.DataSource = cmd.ExecuteReader
DataGrid1.DataBind()
connTSA.Close()
End Sub
Sub datagrid1_EditCommand(ByVal s As Object, ByVal e As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
Sub datagrid1_UpdateCommand(ByVal s As Object, ByVal e As
DataGridCommandEventArgs)
Dim intEmpNo As Integer
Dim FirstText as TextBox
If IsValid Then
intEmpNo = DataGrid1.DataKeys(e.Item.ItemIndex)
FirstText = e.Item.FindControl("employee_first_name")
strSql = "Update Employee Set employee_first_name=@employee_first_name
where employee_number=@employee_number"
Dim cmd1 As New OleDbCommand(strSql, connTSA)
dim objParam as OleDBParameter
objparam = cmd1.parameters.add("@employee_number", OleDBType.Integer)
objparam = cmd1.parameters.add("@employee_name", OleDBType.Char)
'cmd1.Parameters.Add("@employee_first_name", FirstText)
'cmd1.Parameters.Add("@employee_number", intEmpNo)
connTSA.Open()
cmd1.ExecuteNonQuery()
connTSA.Close()
DataGrid1.EditItemIndex = -1
BindDataGrid()
End If
End Sub
Sub datagrid1_CancelCommand(ByVal s As Object, ByVal e As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = -1
BindDataGrid()
End Sub
</Script>
<html>
<head><title>DataGridEditTemplate.aspx</title></head>
<body>
<form runat="server">
<aspataGrid
ID="DataGrid1"
OnEditCommand="DataGrid1_EditCommand"
OnUpdateCommand="DataGrid1_Updatecommand"
OnCancelCommand="DataGrid1_CancelCommand"
DataKeyField="employee_number"
AutoGenerateColumns="False"
CellPadding="10"
HeaderStyle-BackColor="Salmon"
Runat="Server">
<HeaderStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:EditCommandColumn
ButtonType="LinkButton"
CancelText="Cancel"
EditText="Edit"
HeaderText="Edit"
UpdateText="Update"
Visible="True"/>
<asp:BoundColumn DataField="employee_number"
HeaderText="Employee Number"
ReadOnly="true"
Visible="True"/>
<asp:BoundColumn DataField="employee_first_name"
HeaderText="First Name"
ReadOnly="false"
Visible="True"/>
<asp:BoundColumn DataField="employee_last_name"
HeaderText="Last Name"
ReadOnly="false"
Visible="True"/>
<asp:BoundColumn DataField="task_assigner"
HeaderText="Task Assigner"
ReadOnly="false"
Visible="True"/>
</Columns>
</aspataGrid>
</form>
</body>
</html>
problems updating a database through a data grid. below is the error I
receive and below that the code.
Thanks in Advance,
Elena.
Parameter @employee_first_name has no default value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Parameter
@employee_first_name has no default value.
Source Error:
Line 48: cmd1.Parameters.Add("@employee_number", intEmpNo)
Line 49: connTSA.Open()
Line 50: cmd1.ExecuteNonQuery()
Line 51: connTSA.Close()
CODE:
<%@ Page Language="VB" ContentType="text/html" Debug="true" %><%@ Import
Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<Script Runat="Server">
Dim connTSA As OleDbConnection
Dim conStr As String
Dim strSql As String
Sub Page_Load()
conStr = "Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\TSATrackerDB.mdb"
connTSA = New OleDbConnection(conStr)
If Not IsPostBack Then
BindDataGrid()
End If
End Sub
Sub BindDataGrid()
connTSA.Open()
strSql = "Select * from Employee"
Dim cmd As New OleDbCommand(strSql, connTSA)
DataGrid1.DataSource = cmd.ExecuteReader
DataGrid1.DataBind()
connTSA.Close()
End Sub
Sub datagrid1_EditCommand(ByVal s As Object, ByVal e As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
Sub datagrid1_UpdateCommand(ByVal s As Object, ByVal e As
DataGridCommandEventArgs)
Dim intEmpNo As Integer
Dim FirstText as TextBox
If IsValid Then
intEmpNo = DataGrid1.DataKeys(e.Item.ItemIndex)
FirstText = e.Item.FindControl("employee_first_name")
strSql = "Update Employee Set employee_first_name=@employee_first_name
where employee_number=@employee_number"
Dim cmd1 As New OleDbCommand(strSql, connTSA)
dim objParam as OleDBParameter
objparam = cmd1.parameters.add("@employee_number", OleDBType.Integer)
objparam = cmd1.parameters.add("@employee_name", OleDBType.Char)
'cmd1.Parameters.Add("@employee_first_name", FirstText)
'cmd1.Parameters.Add("@employee_number", intEmpNo)
connTSA.Open()
cmd1.ExecuteNonQuery()
connTSA.Close()
DataGrid1.EditItemIndex = -1
BindDataGrid()
End If
End Sub
Sub datagrid1_CancelCommand(ByVal s As Object, ByVal e As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = -1
BindDataGrid()
End Sub
</Script>
<html>
<head><title>DataGridEditTemplate.aspx</title></head>
<body>
<form runat="server">
<aspataGrid
ID="DataGrid1"
OnEditCommand="DataGrid1_EditCommand"
OnUpdateCommand="DataGrid1_Updatecommand"
OnCancelCommand="DataGrid1_CancelCommand"
DataKeyField="employee_number"
AutoGenerateColumns="False"
CellPadding="10"
HeaderStyle-BackColor="Salmon"
Runat="Server">
<HeaderStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:EditCommandColumn
ButtonType="LinkButton"
CancelText="Cancel"
EditText="Edit"
HeaderText="Edit"
UpdateText="Update"
Visible="True"/>
<asp:BoundColumn DataField="employee_number"
HeaderText="Employee Number"
ReadOnly="true"
Visible="True"/>
<asp:BoundColumn DataField="employee_first_name"
HeaderText="First Name"
ReadOnly="false"
Visible="True"/>
<asp:BoundColumn DataField="employee_last_name"
HeaderText="Last Name"
ReadOnly="false"
Visible="True"/>
<asp:BoundColumn DataField="task_assigner"
HeaderText="Task Assigner"
ReadOnly="false"
Visible="True"/>
</Columns>
</aspataGrid>
</form>
</body>
</html>