Error: Object doesn't support this property or method

Discussion in 'ASP .Net' started by Roman, Jan 4, 2004.

  1. Roman

    Roman Guest

    I'm trying to create the form which would allow data entry
    to the Client table, as well as modification and deletion
    of existing data rows. For some reason the DataGrid part
    of functionality stops working when I include data entry
    fields to the form: I click on Delete or Edit inside of
    DataGrid and get this error: "Error: Object doesn't
    support this property or method"

    If I remove data entry fields from the form - DataGrid
    allows to delete or update data inside of itself. I don't
    see how the data entry fields can cause DataGrid stop
    working. I'm going to include my code here.

    If you have an example of what I'm trying to do here,
    please let me know where I can find it. (I'm just trying
    to create a form to view, insert, update, and delete data
    from the table.)

    Thank you.

    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Data" %>
    <%@ page debug="true" trace="true" %>
    <HTML>
    <script language="C#" runat="server">

    SqlConnection myConnection;

    protected void Page_Load(Object Src, EventArgs E)
    {
    myConnection = new SqlConnection("user
    id=sa;password=barrom;initial catalog=propmgmt;data
    source=COMP1;Connect Timeout=30");

    if (!IsPostBack)
    BindGrid();
    }

    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_Delete(Object sender,
    DataGridCommandEventArgs e)
    {
    String deleteCmd = "DELETE from client where
    client_id = @Client_Id";

    SqlCommand myCommand = new SqlCommand(deleteCmd,
    myConnection);
    myCommand.Parameters.Add(new SqlParameter
    ("@Client_Id", SqlDbType.NVarChar, 10));
    myCommand.Parameters["@Client_Id"].Value =
    MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

    myCommand.Connection.Open();

    try
    {
    myCommand.ExecuteNonQuery();
    Message.InnerHtml = "<b>Record
    Deleted</b><br>" + deleteCmd;
    }
    catch (SqlException)
    {
    Message.InnerHtml = "ERROR: Could not delete
    record";
    Message.Style["color"] = "red";
    }

    myCommand.Connection.Close();

    BindGrid();
    }

    public void MyDataGrid_Update(Object sender,
    DataGridCommandEventArgs E)
    {
    String updateCmd = "UPDATE client SET client_id =
    @Client_Id, adr_id = @Adr_Id, locale_id = @Locale_id,
    status = @Status, "
    + "start_date = @Start_date, expir_date =
    @Expir_date, currency = @Currency where client_id =
    @Client_Id";

    SqlCommand myCommand = new SqlCommand(updateCmd,
    myConnection);

    myCommand.Parameters.Add(new SqlParameter
    ("@Client_Id", SqlDbType.NVarChar, 10));
    myCommand.Parameters.Add(new SqlParameter
    ("@Adr_Id", SqlDbType.NVarChar, 20));
    myCommand.Parameters.Add(new SqlParameter
    ("@Locale_id", SqlDbType.NVarChar, 20));
    myCommand.Parameters.Add(new SqlParameter
    ("@Status", SqlDbType.NChar, 1));
    myCommand.Parameters.Add(new SqlParameter
    ("@Start_date", SqlDbType.NVarChar, 19));
    myCommand.Parameters.Add(new SqlParameter
    ("@Expir_date", SqlDbType.NVarChar, 19));
    myCommand.Parameters.Add(new SqlParameter
    ("@Currency", SqlDbType.NVarChar, 10));

    myCommand.Parameters["@Client_Id"].Value =
    MyDataGrid.DataKeys[E.Item.ItemIndex];

    String[] cols =
    {"@Client_Id","@Adr_Id","@Locale_id","@Status","@Start_date
    ","@Expir_date","@Currency"};

    int numCols = E.Item.Cells.Count;
    for (int i=2; i<numCols-1; i++) //skip first,
    second and last column
    {
    String colvalue =((TextBox)E.Item.Cells
    [i+1].Controls[0]).Text;

    // check for null values in required fields
    //if (i<7 && colvalue == "")
    //{
    // Message.InnerHtml = "ERROR: Null values
    not allowed for Client ID, Name or Status";
    // Message.Style["color"] = "red";
    // return;
    //}
    myCommand.Parameters[cols[i-1]].Value =
    Server.HtmlEncode(colvalue);
    }

    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();
    }

    private void MyDataGrid_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)
    {
    for (int i = 0; i < e.Item.Controls.Count; i++)
    {
    try
    {
    if (e.Item.Controls.Controls
    [0].GetType().ToString()
    == "System.Web.UI.WebControls.TextBox")
    {
    TextBox tb = (TextBox)
    e.Item.Controls.Controls[0];
    tb.Text = Server.HtmlDecode
    (tb.Text);
    }
    }
    catch
    {

    }
    }
    }
    }

    public void AddClient_Click(Object sender, EventArgs
    E)
    {
    if (client_id.Value == "" || adr_id.Value == "" ||
    locale_id.Value == "")
    {
    Message.InnerHtml = "ERROR: Null values not
    allowed for Client ID, Address ID, or Locale ID";
    Message.Style["color"] = "red";
    BindGrid();
    return;
    }

    String insertCmd = "insert into client " +
    " (client_id, adr_id, locale_id, status,
    start_date, expir_date, currency) values " +
    "(@client_id,@adr_id,@locale_id,@status,@start_date
    ,@expir_date,@currency)";

    SqlCommand myCommand = new SqlCommand(insertCmd,
    myConnection);

    myCommand.Parameters.Add(new SqlParameter
    ("@client_id", SqlDbType.NVarChar, 20));
    myCommand.Parameters["@client_id"].Value =
    Server.HtmlEncode(client_id.Value);

    myCommand.Parameters.Add(new SqlParameter
    ("@adr_id", SqlDbType.NVarChar, 20));
    myCommand.Parameters["@adr_id"].Value =
    Server.HtmlEncode(adr_id.Value);

    myCommand.Parameters.Add(new SqlParameter
    ("@locale_id", SqlDbType.NVarChar, 20));
    myCommand.Parameters["@locale_id"].Value =
    Server.HtmlEncode(locale_id.Value);

    myCommand.Parameters.Add(new SqlParameter
    ("@status", SqlDbType.NChar, 1));
    myCommand.Parameters["@status"].Value =
    Server.HtmlEncode(status.Value);

    myCommand.Parameters.Add(new SqlParameter
    ("@start_date", SqlDbType.NVarChar, 19));
    myCommand.Parameters["@start_date"].Value =
    Server.HtmlEncode(start_date.Value);

    myCommand.Parameters.Add(new SqlParameter
    ("@expir_date", SqlDbType.NVarChar, 19));
    myCommand.Parameters["@expir_date"].Value =
    Server.HtmlEncode(expir_date.Value);

    myCommand.Parameters.Add(new SqlParameter
    ("@currency", SqlDbType.NVarChar, 10));
    myCommand.Parameters["@currency"].Value =
    Server.HtmlEncode(currency.Value);

    myCommand.Connection.Open();

    try
    {
    myCommand.ExecuteNonQuery();
    Message.InnerHtml = "<b>Record Added</b><br>"
    + insertCmd.ToString();
    }
    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 add
    record, please ensure the fields are correctly filled out";
    Message.Style["color"] = "red";
    }

    myCommand.Connection.Close();

    BindGrid();
    }

    public void BindGrid()
    {
    SqlDataAdapter myCommand = new SqlDataAdapter
    ("select * from client", myConnection);

    DataSet ds = new DataSet();
    myCommand.Fill(ds, "client");

    MyDataGrid.DataSource=ds.Tables
    ["client"].DefaultView;
    MyDataGrid.DataBind();
    }
    </script>
    <body style="FONT: 10pt verdana"
    background="bg_blond.jpg">
    <form runat="server">
    <table width="95%">
    <tr>

    <td nowrap>Client ID</td>

    <td width="163"><input type="text" id="client_id"
    runat="server" NAME="client_id"></td>

    <td nowrap>Address ID</td>

    <td width="163"><input type="text" id="adr_id"
    runat="server" NAME="adr_id"></td>

    </tr>

    <tr>

    <td nowrap>Locale ID</td>

    <td width="163"><input type="text" id="locale_id"
    runat="server" NAME="locale_id"></td>

    </tr>

    <tr>

    <td><hr size="3">

    </td>

    <td><hr size="3">

    </td>

    <td><hr size="3">

    </td>

    <td><hr size="3">

    </td>

    </tr>

    <tr>

    <td nowrap>Status</td>

    <td width="163"><input type="text" id="status"
    runat="server" NAME="status"></td>

    <td nowrap>Start Date</td>

    <td><input type="text" id="start_date"
    runat="server" NAME="start_date">

    <asp:CompareValidator Runat="server"
    ID="startDateCompareValidator"
    ControlToValidate="start_date" Display="Dynamic"

    Operator="DataTypeCheck"
    Type="Date" ErrorMessage="Must enter a valid date." />

    <asp:RangeValidator Runat="server"
    ID="startDateRangeValidator"
    ControlToValidate="start_date" Display="Dynamic"

    MinimumValue="2004-1-1"
    MaximumValue="9999-12-31" Type="Date" ErrorMessage="Date
    entered is out of range." />

    </td>

    </tr>

    <tr>

    <td nowrap>Currency Code</td>

    <td><input type="text" id="currency"
    runat="server" NAME="currency"></td>

    <td nowrap>Expiration Date</td>

    <td width="163"><input type="text" id="expir_date"
    runat="server" NAME="expir_date">

    <asp:CompareValidator Runat="server"
    ID="exprDateCompareValidator"
    ControlToValidate="expir_date" Display="Dynamic"

    Operator="DataTypeCheck"
    Type="Date" ErrorMessage="Must enter a valid date." />

    <asp:RangeValidator Runat="server"
    ID="exprDateRangeValidator" ControlToValidate="expir_date"
    Display="Dynamic"

    MinimumValue="2004-1-1"
    MaximumValue="9999-12-31" Type="Date" ErrorMessage="Date
    entered is out of range." />

    </td>

    </tr>

    <tr>

    <td></td>

    <td width="163">

    <input type="submit"
    OnServerClick="AddClient_Click" value="Add Client"
    runat="server"

    ID="submit" NAME="submit">

    </td>

    </tr>

    <tr>

    <td colspan="2" align="center" width="295">

    <span id="Message" runat="server"></span>

    </td>

    </tr>
    </table>
    <br>

    <asp:DataGrid id="MyDataGrid" runat="server"
    Width="800" BackColor="#ccccff" BorderColor="black"

    ShowFooter="false" CellPadding="3" CellSpacing="1"
    Font-Name="Verdana" Font-Size="8pt"
    OnItemDataBound="MyDataGrid_ItemDataBound"

    OnDeleteCommand="MyDataGrid_Delete"
    OnEditCommand="MyDataGrid_Edit"
    OnCancelCommand="MyDataGrid_Cancel"

    OnUpdateCommand="MyDataGrid_Update"
    DataKeyField="client_id" AutoGenerateColumns="False"
    EnableViewState="true"

    Visible="true">

    <EditItemStyle BackColor="#33CCFF"></EditItemStyle>

    <AlternatingItemStyle
    BackColor="#99CCFF"></AlternatingItemStyle>

    <HeaderStyle Font-Bold="True" ForeColor="White"
    BackColor="#0066FF"></HeaderStyle>

    <Columns>

    <asp:ButtonColumn Text="Delete"
    CommandName="Delete"></asp:ButtonColumn>

    <asp:EditCommandColumn ButtonType="LinkButton"
    UpdateText="Update" CancelText="Cancel" EditText="Edit">

    <ItemStyle Wrap="False"></ItemStyle>

    </asp:EditCommandColumn>

    <asp:BoundColumn DataField="client_id"
    SortExpression="client_id" ReadOnly="True"
    HeaderText="client_id">

    <ItemStyle Wrap="False"></ItemStyle>

    </asp:BoundColumn>

    <asp:BoundColumn DataField="adr_id"
    SortExpression="adr_id"
    HeaderText="adr_id"></asp:BoundColumn>

    <asp:BoundColumn DataField="locale_id"
    SortExpression="locale_id"
    HeaderText="locale_id"></asp:BoundColumn>

    <asp:BoundColumn DataField="status"
    SortExpression="status"
    HeaderText="status"></asp:BoundColumn>

    <asp:BoundColumn DataField="start_date"
    SortExpression="start_date"
    HeaderText="start_date"></asp:BoundColumn>

    <asp:BoundColumn DataField="expir_date"
    SortExpression="expir_date"
    HeaderText="expir_date"></asp:BoundColumn>

    <asp:BoundColumn DataField="currency"
    SortExpression="currency"
    HeaderText="currency"></asp:BoundColumn>

    </Columns>

    </asp:DataGrid>
    </form>
    </body>
    </HTML>
     
    Roman, Jan 4, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mygoogleac
    Replies:
    0
    Views:
    182
    mygoogleac
    Oct 2, 2003
  2. Matt
    Replies:
    3
    Views:
    201
  3. news.onetel.net.uk
    Replies:
    3
    Views:
    216
    Michael Winter
    Aug 9, 2004
  4. SmittyBroham
    Replies:
    2
    Views:
    126
    SmittyBroham
    Jan 7, 2005
  5. Drew
    Replies:
    1
    Views:
    104
    Martin Honnen
    Nov 8, 2006
Loading...

Share This Page