Hi,
I am trying to set the InsertCommand of a SqlDataSource prior to a new record being created in DetailsView. It has to be changed because certain fields are not being used in one scenerio and it is causing Null's to be written to the database.
Here is what I have tried:
I set the Insert command on page Load:
protectedvoid Page_Load(object sender,EventArgs e){
SqlDataSource3.InsertCommand =
"Insert into table (name,realName,type,extraValue) VALUES (@.name,@.realName,@.type,@.extraValue)";}
But I don't want to insert the "extraValue if a certain condition is true so I want to change the insert command to SqlDataSource3.InsertCommand ="Insert into table (name,realName,type) VALUES (@.name,@.realName,@.type)
I tried to do that in the following:
protectedvoid DetailsView1_ItemInserted(Object sender, System.Web.UI.WebControls.DetailsViewInsertedEventArgs e){
SqlDataSource3.InsertCommand ="Insert into table (name,realName,type,extraValue) VALUES (@.name,@.realName,@.type,@.extraValue)";
if (e.Exception !=null){
ErrorMessageLabel.Text =
"An error occured while entering this record. Please verify you have entered data in the correct format.";e.ExceptionHandled =
true;Response.Write(e.Exception);
}
GridView1.DataBind();
GridView1.DataBind();
}
But it doesn't seem to change,
Any Help would be appreciated,
Doug
Excuse me, maybe I missed it, but where did you change the insert command of SqlDataSource3? I saw you assign the same string to the insert command of SqlDataSource3 twice:
The first time:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource3.InsertCommand =
"Insert into table (name,realName,type,extraValue) VALUES (@.name,@.realName,@.type,@.extraValue)";
}
And the second time:
protected void DetailsView1_ItemInserted(Object sender, System.Web.UI.WebControls.DetailsViewInsertedEventArgs e)
{
SqlDataSource3.InsertCommand ="Insert into table (name,realName,type,extraValue) VALUES (@.name,@.realName,@.type,@.extraValue)";
|||Sorry I made the same reply by mistake
|||Some logic for this:
On you page load event:
IF condition1 is true THEN
SqlDataSource3.SelectCommand=" SELECT name,realName,type,extraValue FROM table"
SqlDataSource3.InsertCommand =
"Insert into table (name,realName,type,extraValue) VALUES (@.name,@.realName,@.type,@.extraValue)"
ELSE
SqlDataSource3.SelectCommand ="SELECT name,realName,type FROM table"
SqlDataSource3.InsertCommand ="Insert into table (name,realName,type) VALUES (@.name,@.realName,@.type)
END IF
}
|||Sorry, the second should be:
protected void DetailsView1_ItemInserted(Object sender, System.Web.UI.WebControls.DetailsViewInsertedEventArgs e)
{
SqlDataSource3.InsertCommand ="Insert into table (name,realName,type) VALUES (@.name,@.realName,@.type)
That is actually what I tried. But it doesn't seem to change..
|||Thanks for taking a look,
Unfortunately the condition doesn't change on page load, it changes when a radio button is clicked within this page:
protectedvoid Type_Changed(object sender,EventArgs e){
string aha = ((RadioButtonList)DetailsView1.FindControl("type")).SelectedItem.Value;if (aha =="Mortgage"){
DetailsView1.Fields[6].Visible =
true;DetailsView1.Fields[7].Visible =
true;DetailsView1.Fields[8].Visible =
true;}
else{
DetailsView1.Fields[6].Visible =
false;DetailsView1.Fields[7].Visible =
false;DetailsView1.Fields[8].Visible =
false;}
}
I tried to change the Sql Insert command here but it doesn't seem to work.
Thanks again,
Doug
No comments:
Post a Comment