Hello all, basically I would want to select a specific field and update it. However when I click Update, I would want to see the data that I selected not just a blank field. I am using a View( I joined 2 tables), so is it possible to update the view through the datagrid or do I have to update them separately?

My view is already mapped but I cannot figure out the update process.

This is what I have so far:

protected void bindGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }
        protected void bindGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            bool flag = false;
            TextBox txtAccountID = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("AccountID");
            TextBox txtCountryCode = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("CountryCode");
         TextBox txtType = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("Type");
            this.Validate();
            //Do I need an Adapter??

       }






 <asp:GridView ID="bindGridView" runat="server"

                     Width="797px" 
                    AutoGenerateColumns="False" AllowSorting="True" selectedindex="0" 
                    AutoGenerateEditButton="True" onrowediting="bindGridView_RowEditing" > 

                    <Columns>
                    <asp:BoundField Visible="true" DataField="AccountID" HeaderText="AccountID" />
                    <asp:BoundField Visible="true" DataField="CountryCode" HeaderText="CountryCode" />
                    <asp:BoundField Visible="true" DataField="Region" HeaderText="Region" />
                    <asp:BoundField Visible="true" DataField="Account" HeaderText="Account" />
                    <asp:BoundField Visible="true" DataField="Type" HeaderText="Type" />


                </Columns>



                </asp:GridView>

This is with what I came up so far. Is it possible to update a VIEW using this way? I couldn't create adapters so I kidna gave up on them. I am getting an error about unable to handle RowUpdating.

 protected void bindGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)  

    {           


                 GridViewRow row = (GridViewRow)bindGridView.Rows[e.RowIndex];
                 Label lbl = (Label)row.FindControl("lblid"); 
                 TextBox txtAccountID = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("AccountID");
                 TextBox txtCountryCode = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("CountryCode");
                 TextBox txtRegion = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("Region");
                 TextBox txtAccount = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("Account");
                 TextBox txtType = (TextBox)bindGridView.Rows[e.RowIndex].FindControl("Type");
               bindGridView.EditIndex = -1;
               SqlCommand cmd = new SqlCommand("UPDATE ACCOUNT_DATA SET AccountID=" + txtAccountID.Text + " , CountryCode='" + txtCountryCode.Text +
                   ", Region = '" + txtRegion + ", Account ='" + txtAccount + " Type = '" + txtType
                   + "' where rowid="  + lbl.Text + "");  
               cmd.ExecuteNonQuery();
               DataBind();
               this.Validate();

I fixed the errors, the only problem now is can I actually update the VIEW straight away or do I need 2 update statements for the 2 tables? Any help will be highly appreciated.

SqlCommand cmd = new SqlCommand("UPDATE ACCOUNT_DATA SET AccountID=" + txtAccountID.Text + " , CountryCode='" + txtCountryCode.Text +15.                   ", Region = '" + txtRegion + ", Account ='" + txtAccount + " Type = '" + txtType16.                   + "' where rowid="  + lbl.Text + "");  

Some views allow updating, but I think it depends on whether it can be traced back to the actual records.

 protected void bindGridView_RowDeleting(object sender,GridViewDeleteEventArgs e)
     {
      var sessionFactory = (ISessionFactory)Application[Constants.Application.SessionFactory];

      using (var session = sessionFactory.OpenSession())
      {
          if (bindGridView.Rows.Count > 0)
          {
              int selectedIndex = bindGridView.Rows[0].RowIndex;
              // gets the RowID from the first column in the grid  
              int rowID = int.Parse(bindGridView.Rows[0].RowIndex.ToString());
              //string sql = ("DELETE FROM ACCOUNT_DATA WHERE ACCOUNTID = @RowID");
              var delete = session.CreateQuery("DELETE FROM ACCOUNT_DATA WHERE ACCOUNTID = RowIndex");
              bindGridView.DataSource = delete;

          }
      }

       I am trying to Do the Deletion one now. I am getting error that ACCOUNT_DATA[ DELETE FROM ACCOUNT_DATA] has not been mapped. Even though I've mapped my view Account_DATA...No idea how to solve this one out..
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.