Автор: Lihonosov
Дата сообщения: 29.11.2007 14:09
Заменил везде в тексте SQL на Sql и ошибок с этим больше нет, но на другом застрял.
Решил все переделать на работу с Access.
Код:
Код: <%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Text"%>
<html> <script language="C#" runat="server">
OleDbConnection myConnection;
protected void Page_Load(Object Src, EventArgs E )
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source="+MapPath("pubs.mdb");
OleDbConnection myConnection=new OleDbConnection(strConnection);
if (!IsPostBack)
BindGrid();
}
public void AddAuthor_Click(Object sender, EventArgs E)
{
if (au_id.Value == "" || au_fname.Value == "" ||au_lname.Value == "" || phone.Value == "")
{
Message.InnerHtml = "ОШИБКА: Пустое значение не допустимо для полей Author ID, Name или Phone";
Message.Style["color"] = "red"; BindGrid(); return;
}
String insertCmd = "insert into Authors values (@Id, @LName, @FName, @Phone, @Address, @City,@State, @Zip, @Contract)";
OleDbCommand myCommand = new OleDbCommand(insertCmd,myConnection); myCommand.Parameters.Add(new OleDbParameter("@Id",OleDbType.VarChar, 11)); myCommand.Parameters["@Id"].Value = au_id.Value;
myCommand.Parameters.Add(new OleDbParameter("@LName",OleDbType.VarChar, 40));
myCommand.Parameters["@LName"].Value = au_lname.Value; myCommand.Parameters.Add(new OleDbParameter("@FName",OleDbType.VarChar, 20));
myCommand.Parameters["@FName"].Value = au_fname.Value; myCommand.Parameters.Add(new OleDbParameter("@Phone", OleDbType.Char, 12)); myCommand.Parameters["@Phone"].Value = phone.Value;
myCommand.Parameters.Add(new OleDbParameter("@Address",OleDbType.VarChar, 40));
myCommand.Parameters["@Address"].Value = address.Value; myCommand.Parameters.Add(new OleDbParameter("@City",OleDbType.VarChar, 20));
myCommand.Parameters["@City"].Value = city.Value;
myCommand.Parameters.Add(new OleDbParameter("@State",OleDbType.Char, 2)); myCommand.Parameters["@State"].Value = state.Value;
myCommand.Parameters.Add(new OleDbParameter("@Zip",OleDbType.Char, 5)); myCommand.Parameters["@Zip"].Value = zip.Value;
myCommand.Parameters.Add(new OleDbParameter("@Contract",OleDbType.VarChar,1)); myCommand.Parameters["@Contract"].Value = contract.Value;
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source="+MapPath("pubs.mdb");
OleDbConnection myConnection2=new OleDbConnection(strConnection);
OleDbCommand myCommand2 = new OleDbCommand(insertCmd.ToString(), myConnection2);
myCommand2.Connection.Open();
try
{
myCommand2.ExecuteReader();
Message.InnerHtml = "<b>Record Added</b><br>" + insertCmd.ToString();
}
catch(OleDbException e)
{
if (e.ErrorCode == 2627)
Message.InnerHtml = "ОШИБКА: Запись с таким первичным ключом уже существует ";
else
Message.InnerHtml = "ОШИБКА: Невозможно добавить запись,пожалуйста, убедитесь, что поля правильно заполнены";
Message.Style["color"] = "red";
}
myCommand2.Connection.Close();
BindGrid();
}
public void BindGrid()
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=" + MapPath("pubs.mdb");
OleDbConnection myConnection=new OleDbConnection(strConnection);
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from Authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");
MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
}
</script>
<body style="font: 10pt verdana"> <form runat="server"> <h3><font face="Verdana">Inserting a Row of Data</font></h3> <table width="95%"> <tr> <td valign="top"> <ASP:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" MaintainState="false" /> </td> <td valign="top"> <table style="font: 8pt verdana"> <tr> <td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">Add a New Author:</td> </tr> <tr> <td nowrap>Author ID: </td> <td style="width: 158px"><input type="text" id="au_id" value="000-00-0000" runat="server"></td> </tr> <tr> <td nowrap>Last Name: </td> <td style="width: 158px"><input type="text" id="au_lname" value="Doe" runat="server"></td> </tr> <tr nowrap> <td>First Name: </td> <td style="width: 158px"><input type="text" id="au_fname" value="John" runat="server"></td> </tr> <tr> <td>Phone: </td> <td style="width: 158px"><input type="text" id="phone" value="808 555-5555" runat="server"></td> </tr> <tr> <td>Address: </td> <td style="width: 158px"><input type="text" id="address" value="One Microsoft Way" runat="server"></td> </tr> <tr> <td>City: </td> <td style="width: 158px"><input type="text" id="city" value="Redmond" runat="server"></td> </tr> <tr> <td>State: </td> <td style="width: 158px"> <select id="state" runat="server"> <option>CA</option> <option>IN</option> <option>KS</option> <option>MD</option> <option>MI</option> <option>OR</option> <option>TN</option> <option>UT</option> </select> </td> </tr> <tr> <td nowrap>Zip Code: </td> <td style="width: 158px"><input type="text" id="zip" value="98005" runat="server"></td> </tr> <tr> <td style="height: 24px">Contract:</td> <td style="height: 24px; width: 158px;"> <select id="contract" runat="server"> <option value="0">False</option> <option value="1">True</option> </select> </td> </tr> <tr> <td></td> <td style="padding-top:15; width: 158px;"> <input type="submit" OnServerClick="AddAuthor_Click" value="Add Author" runat="server"> </td> </tr> <tr> <td colspan="2" style="padding-top:15" align="center"> <span id="Message" MaintainState="false" style="font: arial 11pt;" runat="server"/> </td> </tr> </table> </td> </tr> </table> </form> </body> </html>