Tuesday, February 14, 2012

Button1_click code behind

Hi, I have the following code which creates a dropdown box with values from the database.

1) I would like an sql statement to take place to update the database. When the button is clicked I would like the database to update so 'doc_area_default' is changed to '1' for the value that is selected via the dropdown. All others should be changed to '0'. Then direct the user to ManageAreas.aspx with a success message.

2) I would like to be able to do this in the code behind page (cs). Using the "protectedvoid Button1_Click(object sender,EventArgs e)" method. I'm not sure how to do the database connection like I would in the aspx page either.

Thanks for any help you can give!

<

asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="doc_area_name"DataValueField="doc_area_id"></asp:DropDownList><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:CPS_docshareConnectionString %>"SelectCommand="SELECT * FROM [document_area] WHERE [doc_area_type] = 1 ORDER BY [doc_area_default] DESC"></asp:SqlDataSource>

<

asp:ButtonID="Button1"runat="server"Text="Update"OnClick="Button1_Click"/>

Dearmlawton40 .

I guess u can use store procedure to do this.

Try to use these code

//Create procedure

create proc sp_update

@.doc_area_id int

as

update document_area set doc_area_type=1 where [doc_area_id] = @.doc_area_id

update document_area set doc_area_type=0 where [doc_area_id] <> @.doc_area_id

go

And Code behind your page

public void Button1_Click()
{


SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["CPS_docshareConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("sp_update", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@.doc_area_id",DropDownList1.selectedvalue));
cmd.Connection.Open();
cmd.ExecuteNonQuery();

Response.Redirect("ManageAreas.aspx");
}

Hope that it will help you..

No comments:

Post a Comment