0

Hi! I'm using C# and SQL Server.

I need to take data from one table, display it to the user, allow the user to modify anything, and then when they click save, have the record added to a new table and deleted from the old.

I have the first table display minimal data in the GridView. When the patron selects the GridView record, I display the full information and a few more controls in a FormView. The user is then able to modify those controls as they wish. I have used the Edit Item Template to display the information.

I've got this working fine. I'm just not sure how I can take that data, add it as a new row into a DB, and delete the old.

I was going to try adding a button to the FormView, and in it's click procedure, inserting and deleting the record. But I cannot figure out how to reference each control in order to do this.


Say I had these fields in the Edit Itemp Template of a FormView called fvUser

<asp:TextBox ID="IDNum" runat="server" Text='<%# Bind("IDNum") %>'></asp:TextBox>
<asp:TextBox ID="txtLN" runat="server" Text='<%# Bind("LN") %>'></asp:TextBox>
<asp:TextBox ID="txtFN" runat="server"></asp:TextBox>
<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeUser" runat="server" onclick="btnChangeUser_Click" />

I want the user to be able to modify everything.
And then, this in the code behind.

protected void btnChangeUser_Click(object sender, EventArgs e)
{
}

I'm not sure how I can reference the controls in the code behind

Thank you.

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by padtes
0

From the code snippet it is not clear to me if the asp:TextBox ID="IDNum" is part of edit template or not.
Also it is not clear if your question is related to DB operation or reading data from page or both.
If it is not part of grid's edit template then values are read from for using int anID = convert.toInt32(IDNum.Text) if it is part of EditItemTemplate, you will need a command rather than a button on the form.

<asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />

Then on server site you handle RowEditing event, rather than your current button click handling.

protected void gvYourGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        TextBox txtIDNum = (TextBox)gvYourGrid.Rows[e.NewEditIndex].FindControl("IDNum");

int anID = convert.toInt32(IDNum.Text)
//get other fields and insert/update etc.
    }

As far database delete and inserts are concerned, how is it any different than any simple insert/delete? Do you mean to create ALL columns from one table (like audit situation) to be moved to other table? In that case triggers are useful.

0

From the code snippet it is not clear to me if the asp:TextBox ID="IDNum" is part of edit template or not.
Also it is not clear if your question is related to DB operation or reading data from page or both.
If it is not part of grid's edit template then values are read from for using int anID = convert.toInt32(IDNum.Text) if it is part of EditItemTemplate, you will need a command rather than a button on the form.

<asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />

Then on server site you handle RowEditing event, rather than your current button click handling.

protected void gvYourGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        TextBox txtIDNum = (TextBox)gvYourGrid.Rows[e.NewEditIndex].FindControl("IDNum");

int anID = convert.toInt32(IDNum.Text)
//get other fields and insert/update etc.
    }

As far database delete and inserts are concerned, how is it any different than any simple insert/delete? Do you mean to create ALL columns from one table (like audit situation) to be moved to other table? In that case triggers are useful.

Yes, it is in the item template.

I want to display the data from the one table, which is populated someother way. A user will then select the record from a grid view.

I have it displaying the data from the selected record in the Form View. But, I want the user to modify the data (or not) and click the button to add it into another table.

If the form is bound to the SQLDataSource for the first table, how can the information be entered into another? I am new to ASP.NET, and I am learning as I go along. It may be something simple that I'm overlooking. Thank you,


And then with deleting the record from the first, I can see how a trigger could do the deletion part.

0

ok, as I said before, you need to see sample code I have in the prior post that handles RowEditing event of the grid where you will save the record using values read from the page that was submited by user.

Further there is .._RowCancelingEdit when user cancels edit, when all you need to do is

your_gv.EditIndex = -1;
      your_gv.ShowFooter = false;  //to hide footer template
      BindGridView();//your own method to rebind your grid as you did in the past

2 more points:
1. Binding: After save, you need to rebind anyway. Creating BindGridView() kind of method will come handy. That should take care of deleted/added records etc.
+ as above, hide footer and set editing Off (EditIndex = -1)

2. Now as far as database delete / inserts are concerned, these need to be simple SQLs you can write in C# OR pass all variabels to a stored procedure and write queries/transaction in the stored procedure. Execute that SP from ASP.net.

Trigger is recomended only if there is simple work like flagging a record or inserting audit message is required. You can insert a record into different table before the original record is deleted (that will be separate topic).

0

Thank you. I'm sorry I wasn't specific enough. Actually, My controls are in a FormView. I'm using the Grid View to give the patron the ability to select the basic info.

But, I could use the informatin from the GridView to populate text boxes that aren't in a FormView when the row is selected. Then, just take that info and send it to the StoredProc.

0

Okay, I'm still having difficulties.

In the SelectedIndexChanging for my GridView, I Have the following code:

TextBox txtTemp = (TextBox)gvNum.Rows[e.NewSelectedIndex].FindControl("Num");

     string strCopyThis = txtTemp.Text.ToString();

     txtTextBoxOnForm.Text = strCopyThis ;

On the middle line, it tells me that Object reference not set to an instance of an object.

I'm not sure what I'm doing wrong. Isn't the instance created above?

0

Okay, I'm still having difficulties.

In the SelectedIndexChanging for my GridView, I Have the following code:

TextBox txtTemp = (TextBox)gvNum.Rows[e.NewSelectedIndex].FindControl("Num");

     string strCopyThis = txtTemp.Text.ToString();

     txtTextBoxOnForm.Text = strCopyThis ;

On the middle line, it tells me that Object reference not set to an instance of an object.

I'm not sure what I'm doing wrong. Isn't the instance created above?

You need to findControl as named, like "IDNum" and not "Num" just the way you named them. Otherwise there is no controlfound and next line you are getting null object (that is txtTemp is null so txtTemp.Text is bombing)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.