Sunday, February 19, 2012

Inserting Records

Hi,

I'm using VWD 2005 Express with SQL Server 2005 Express. I've created a page "create.aspx" which will be used to insert records into a table within the database.

I've used the DetailsView control together with a SqlDataSource control on the create.aspx page.

When I run the create.aspx page it retrieves the first record in the table of the database becuase the SqlDataSource is obviously performing a"Select * from Table" command.

I don't want this to occur. I want an empty page with no records. I know I can set a WHERE clause to a value that will never return any results but I don't beleieve this is an elegant way of doing things becuase I am 'hitting the database' for no reason other than to get the table structure poplutaed into the DetailsView control.

Questions:

1. Am I using the right controls? (in particular the SqlDataSource control)

2. Is there a better way of doing this?

Your advice is most appreciated. Thank you

Try a formview with the defaultmode set to insert.

|||Motley, Thank you - that works just fine|||

Hi, I'm stuck again. Beginners bad luck.

Let's say I have a database with one table e.g. AccountsTable and this table has several fields such as:AccountID, AccountName, AccountDate, AccountActive etc.

I'm using the FormView with the DeafultMode set to Insert with:

<asp:TextBoxID="AccountDateEstablishedTextBox"runat="server"Text='<%# Bind("AccountDateEstablished") %>'></asp:TextBox>

How do I access and assign the database field?

I would like to programmatically take control. Say for example in the Sub Page_Load (not sure if this is where it should go) have something like

AccountDateEstablished = now

But I get the message saying AccountDateEstablished is not declared. Obviously, it's not referring to the same thing as what I have in mind i.e. I think it's looking for a variable in the codebehind file; when what I want is to have some logic that assigns this field in the database the value of 'now' i.e. this moment in time e.g. 21 Feb 2006.

I have the same issue with a CheckBox used for the AccountActive which is a Bit (I assume this means Boolean). All I want is for it to default to 'True' when the page loads

Another issue also closely releated. If I place a Calendar control otside the FormView then I can access the value Calendar1.SelectedDate as a vlaue in the SqlDataSource1 propoerty InsertQuery, choose AccountDateEstablished and choose from 'Control' form the Parameter Source drop down. However, if I place the Calendar control within the Formview then I cannot access this control or value.

Also, if you had a regular aspx page with a button then I could place some logic in the Click event to check for certain conditions etc. How do you do this with the prewritten FormView "Insert" Linkbutton (or Button)?

Thanks

|||

You don't assign the value to the database, the values don't get written to the database until you actually click the insert button.

It looks like you are binding the AccountDateEstablished to the AccountDateEstablishedTextbox, so if you want it to get written when you click insert, you can of course, just do:

AccountDateEstablishedTextbox.text=now

Of course, that let's the user edit the text, if that is not what you wanted, then don't bind it to a textbox, and on page load, you can do something like:

SqlDataSource1.InsertParameters("@.AccountDateEstablished").defaultvalue=now

And make the AccountDateEstablished a Parameter instead of a ControlParameter. You can also set the value during the SqlDataSource1_Inserting event.

Same for the checkbox. If you want the default to be checked, then set it as checked in page load, or if you always want it to default to checked, then just set the property of the checkbox to checked=true.

Try placing the SqlDatasouce within the formview and see if that fixes your calendar issue -- but I'm not an expert on this, I've avoided the FormView controls, etc. so far.

I would go to the code behind page, select FormView in the upper left dropdown, then see all the events I could catch in the upper right dropdown, I'm sure there is one in there that would be helpful.

|||

It looks like you are binding the AccountDateEstablished to the AccountDateEstablishedTextbox,(Yes, the system did that for me) so if you want it to get written when you click insert, you can of course, just do:

AccountDateEstablishedTextbox.text=now

Of course, that let's the user edit the text(That's fine - the easy way for now [I don't know the difference between Parameter and Control Parameter]).

Prior to submitting the post, and yet again upon your response, I have tried this in the code behind under several different combinations (upper left and right drop downs) but I keep getting the squiggly line NAME'AccountDateEstablishedTextbox' is not declared

I even triedFormView1.AccountDateEstablishedTextbox.textwith no joy i.e. I get the same message of not declared. Do I have to try each and every combination? I feel like I am missing something big here

Normally you can access a control properties in code as you mentioned withAccountDateEstablishedTextbox.text=now, but this FormView seems to be behaving differently by 'hiding the textbox control' that it created

The page has a master page, does that have any side effect?Thanks

|||

I would recommend asking that question in the data presentation controls forum. Hopefully they have a better solution for you than the only one I know of which is to do something silly like this:

CType(FormView1.FindControl("AccountDateEstablishedTextbox"),textbox).text=""

I finally realized that the FormView was wasting more of my time with sillyness like that than to just load and insert my own data so I chucked it, and did that instead.

|||

Hi Motley,

Thanks for better suggesion for inserting records. I was also face problems when i inserting hidden values. Finally I did using "e.Command.Parameters["@.EmployeeID"].Value = Profile.EmpID;" using SqlDataSource1_Inserting event.

FormView/DataList controls seems to simple when I see webcast events but it acutally time consuming at real time development. esp Inserting / Display records.

Regards,

Gopinath,

No comments:

Post a Comment