det virker nu, altså Pets projektet :-/ selvom det ikke fungere skide godt.
Jeg er ved at miste lidt tålmodigheden med dette nye smarte, fleksible ASP.Net!! Min koden er lavet til en Sql connection, men nu skal det "konverteres til oledb conn". Jeg er lidt i tvivl om hvorvidt resten skal rettes til eller om det kun relaterer sig til selve forbindelsen.. Jeg vil meget gerne have noget feedback på hvorvidt koden ser fornuftig ud! Jeg udlover ekstra karma... :-) Tak for enhver hjælp..
-------------------------------------------
<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server" debug="true">
OleDbConnection myconnection = null;
OleDbDataReader reader = null;
protected void Page_Load(Object Src, EventArgs E)
{
myconnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath("Economy/Economy.mdb"));
myconnection.Open();
if (!IsPostBack)
BindGrid();
}
public void BindGrid()
{
try
{
String sqlString = "Select Rec_ID, Company.Company, Date, Income, Currency_Inc, Expense, Balance from Economy, Company Where Economy.ID = 1";
OleDbCommand cmd = new OleDbCommand(sqlString, myConnection);
reader = cmd.ExeCuteReader();
datagrid.DataSource = reader;
datagrid.DataBind();
}
// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{
if (Page.IsValid)
{
String updateCmd = "UPDATE Economy SET Rec_Id = @recid, ID = @id, Date = @date, Income = @income, "
+ "Currency_Inc = @currency_inc, Expencse = @expense, Balance = @balance where ID = 1";
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@recid", SqlDbType.NVarChar, 10));
myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar, 1));
myCommand.Parameters.Add(new SqlParameter("@date", SqlDbType.NChar, 10));
myCommand.Parameters.Add(new SqlParameter("@income", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@currency_inc", SqlDbType.NVarChar, 15));
myCommand.Parameters.Add(new SqlParameter("@expense", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@balance", SqlDbType.NVarChar, 25));
myCommand.Parameters["@recid"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
String[] cols = {"id","date","income","currency_inc","expense","balance"};
for (int i=0; i<6; i++)
{
String colvalue = ((System.Web.UI.WebControls.TextBox)
E.Item.FindControl(cols[i])).Text;
myCommand.Parameters["@" + cols[i]].Value = colvalue;
}
myCommand.Parameters["@currency_inc"].Value = ((DropDownList)E.Item.FindControl("Currency_Inc")).SelectedItem.ToString();
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}
myCommand.Connection.Close();
BindGrid();
}
else
{
Message.InnerHtml = "ERROR: Please check each field for error conditions.";
Message.Style["color"] = "red";
}
}
Rec_ID, Company.Company, Date, Income, Currency_Inc, Expense, Balance from Economy, Company Where Economy.ID = 1";
</script>
<html>
<head>
</head>
<body style="FONT: 10pt verdana">
<form runat="server">
<h3><font face="Verdana">Updating a Row of Data w/ Validation</font>
</h3>
<span id="Message" runat="server" enableviewstate="false">
<p>
<ASP:DataGrid id="MyDataGrid" runat="server" AutoGenerateColumns="false" DataKeyField="Rec_Id" OnUpdateCommand="MyDataGrid_Update" OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" CellSpacing="0" CellPadding="3" ShowFooter="false" BorderColor="black" BackColor="#ccccff" Width="800">
<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false" />
<asp:BoundColumn HeaderText="Record Id" SortExpression="Sort_Rec_Id" ReadOnly="True" DataField="Rec_Id" ItemStyle-Wrap="false" />
<asp:BoundColumn HeaderText="Company" SortExpression="Sort_Company" ReadOnly="True" DataField="Company" ItemStyle-Wrap="false" />
<asp:TemplateColumn HeaderText="Date" SortExpression="Sort_Date">
<ItemTemplate>
<asp:Label runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Date") %>' />
</ItemTemplate>
<EditItemTemplate>
<nobr />
<asp:TextBox runat="server" id="Date" Text='<%# DataBinder.Eval(Container.DataItem, "Date") %>' />
<asp:RequiredFieldValidator id="Date_ReqVal" ControlToValidate="Date" Display="Dynamic" Font-Name="Verdana" Font-Size="12" runat="server">
*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="Date_RegEx" ASPClass="RegularExpressionValidator" ControlToValidate="Date" ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">
* Date must be in form: DD-MM-YYYY<br />
</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Income" SortExpression="Sort_Income">
<ItemTemplate>
<asp:Label runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Income") %>' />
</ItemTemplate>
<EditItemTemplate>
<nobr />
<asp:TextBox runat="server" id="Income" Text='<%# DataBinder.Eval(Container.DataItem, "Income") %>' />
<asp:RequiredFieldValidator id="Income_ReqVal" ControlToValidate="Income" Display="Dynamic" Font-Name="Verdana" Font-Size="12" runat="server">
*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="Income_RegEx" ASPClass="RegularExpressionValidator" ControlToValidate="Income" ValidationExpression="
- \d*" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">
* Numbers must be integers<br />
</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Income Currency" SortExpression="Sort_Currency_Inc">
<ItemTemplate>
<asp:Label runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Currency_inc") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Currency_inc") %>' id="Currency_Inc">
<asp:ListItem>EUR</asp:ListItem>
<asp:ListItem>PLN</asp:ListItem>
<asp:ListItem>USD</asp:ListItem>
<asp:ListItem>GBP</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Expense" SortExpression="Sort_Expense">
<ItemTemplate>
<asp:Label runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Expense") %>' />
</ItemTemplate>
<EditItemTemplate>
<nobr />
<asp:TextBox runat="server" id="Expense" Text='<%# DataBinder.Eval(Container.DataItem, "Expense") %>' />
<asp:RequiredFieldValidator id="Expense_ReqVal" ControlToValidate="Expense" Display="Dynamic" Font-Name="Verdana" Font-Size="12" runat="server">
*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="Expense_RegEx" ASPClass="RegularExpressionValidator" ControlToValidate="Expense" ValidationExpression="
- \d*" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">
* Numbers must be integers<br />
</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Balance" SortExpression="Sort_Balance">
<ItemTemplate>
<asp:Label runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Balance") %>' />
</ItemTemplate>
<EditItemTemplate>
<nobr />
<asp:TextBox runat="server" id="Balance" Text='<%# DataBinder.Eval(Container.DataItem, "Balance") %>' />
<asp:RequiredFieldValidator id="Balance_ReqVal" ControlToValidate="Balance" Display="Dynamic" Font-Name="Verdana" Font-Size="12" runat="server">
*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="Balance_RegEx" ASPClass="RegularExpressionValidator" ControlToValidate="Balance" ValidationExpression="
- \d*" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">
* Numbers must be integers <br />
</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASP:DataGrid>
</p>
</span>
</form>
</body>
</html>