Showing posts with label parameters. Show all posts
Showing posts with label parameters. Show all posts

Saturday, March 24, 2012

Trouble using parameters to insert database record

I am having a hell of a time trying to insert a database record into an SQL Database. I am using VS.NET to generate this code, I am not using a dataset. I get the following error:

System.FormatException: Input string was not in a correct format.

I have narrowed that error down to that error down to the int fields, how do I convert the string from the form into an int (I tried Convert.ToInt32() without success)?

I can get the insert the data successfully if use an explicit value (Value = 30) and not (Value = Formfield.Text)

Also how do I get the DB KeyID Field to autogenerate a number, currently if I do not manually enter a value throws a no Null value allowed error?

-------------------------------
this.sqlInsertCommand1.CommandText = @dotnet.itags.org."INSERT INTO Events(KeyID, EventDateTime, EventName, StartLocation, EndLocation, AvailableSeats, Description, RateAdult, RateChild, RateSenior) VALUES (@dotnet.itags.org.KeyID, @dotnet.itags.org.EventDateTime, @dotnet.itags.org.EventName, @dotnet.itags.org.StartLocation, @dotnet.itags.org.EndLocation, @dotnet.itags.org.AvailableSeats, @dotnet.itags.org.Description, @dotnet.itags.org.RateAdult, @dotnet.itags.org.RateChild, @dotnet.itags.org.RateSenior); SELECT KeyID, EventDateTime, EventName, StartLocation, EndLocation, AvailableSeats, Description, RateAdult, RateChild, RateSenior, EventID FROM Events WHERE KeyID = @dotnet.itags.org.KeyID";

this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.KeyID", System.Data.SqlDbType.Int, 4);
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.EventDateTime", System.Data.SqlDbType.SmallDateTime, 4).Value = System.DateTime.Now;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.AvailableSeats", System.Data.SqlDbType.Int, 4).Value = 21;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.EndLocation", System.Data.SqlDbType.NVarChar, 50).Value = EndLocation.Text;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.EventName", System.Data.SqlDbType.NVarChar, 50).Value = EventName.Text;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.RateAdult", System.Data.SqlDbType.Int, 4).Value = RateAdult.Text;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.RateChild", System.Data.SqlDbType.Int, 4).Value = RateChild.Text;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.RateSenior", System.Data.SqlDbType.Int, 4).Value = RateSenior.Text;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.StartLocation", System.Data.SqlDbType.NVarChar, 50).Value = StartLocation.Text;
this.sqlInsertCommand1.Parameters.Add("@dotnet.itags.org.Description", System.Data.SqlDbType.Text, 16).Value = Description.Text;

Thanks, Justin.Example on how to parse a text (must of course be number only entry, and cannot be an empty string) into an Integer:


sqlInsertCommand1.Parameters.Add("@.RateAdult", SqlDbType.Int, 4).Value = int.Parse(RateAdult.Text);

You should of course not set the KeyID field manually. This is taken care of by the DataBase when designing your Table:

Identity = Yes
Identity Seed = 1
Identity Increment = 1

Thanks Andre,

I have set the key ID field to "Identity = Yes" but I am not not sure what to do in the code with the KeyID Value. If I do not declare a value I get a non-null error.

Any ideas?

Can you recommend some good articles/tutorials for adding data to an MS SQL DB?

Thanks, Justin.
When inserting you need not worry about the KeyID value as it is created when the data is inserted into the DataBase. When updating or deleting (or selecting a certain record based on an ID) you need to get the ID value from Database when you process your query to get the Data you need to display.

If you are using a DataGrid or a DataList to present the Data, you'll use the the DataKeyField property of the DataGrid/DataList (the Repeater Control does not have this Property) to store the id:


DataKeyField = "KeyID"

When you update (or delete), you'll get the necessary ID like this:

int keyID = (int)myDataGrid.DataKeys[e.Item.ItemIndex];

Now you have the correct id for updating or deleting the correct record.
Thanks for the help.

As instructed by your first reply I tried using int.Parse() but received the same error I added an if statement to make sure the form variable was not empty.

Also, I removed all traces of keyID from the insert statment but I am still getting no nulls allowed error.

Thank you for your patience as I am still learning. I appreciate the guidance.

Thanks, Justin.
You will need to show us your Table design. What are the fields of the Table you are trying to insert into? What Type of data do they accept, do they accept 'null' (can they be empty) values or not? Remember that a 'null' value is NOT the same thing as an empty string ("").

"I can get the insert the data successfully if use an explicit value (Value = 30) and not (Value = Formfield.Text)"

If 'Formfield.Text' is empty, you cannot parse it using 'int.Parse(Formfield.Text)'. That will give you an exception. What you can do is e.g. use:


int formfield = 0;

if (Formfield.Text.Trim().Length > 0)
formfield = int.Parse(Formfield.Text.Trim());

sqlInsertCommand1.Parameters.Add("@.someValue", System.Data.SqlDbType.Int, 4).Value = formfield;


This way you ensure that if the particular formfield has not been assigned a value by the user, the value will be '0'.

If there are fields in your Table that by design do not accept 'null' values, you must insert some kind of value. Either by setting a default value in your table design, or by making sure that a valid value is entered by the User, using .Net Validator Controls.
Well, I finally fixed the problem.

VS.NET puts the insert statement in the InitializeComponent() class and I thought if I put the execute command in submit's event handeler it would execute the insert statement from the InitializeComponent() but it wasn't.

I put the insert command in the event handeler and now it's working.

Thank you very much for your patience.

Thanks, Justin.

Thursday, March 22, 2012

Trouble with sequence of page build.

Instead of using Hyperlinks and URL / Response.QueryString() parameters to
pass state around in my app, I am using LinkButtons with a corresponding
command event and command arguments.
This page has LinkButtons in a menu down the left hand side, that when
clicked causes the main content of the page to change. The main content of
the page has linkbuttons as well. I am having trouble with the linkbuttons
in the maincontent area keeping their associated command event wired
together.
The linkbuttons in the left menu get built in the page load of the initial
load and all subsequent postbacks (these work fine).
When a left menu linkbutton is clicked it causes a postback,
Page_Load()
BuildLeftMenu() is run in page_load
I don't run BuildMainContent() in the page_load because I don't have the new
TopicID yet.
MyLinkButton_Command() fires next, where I capture the command argument
which tells me the TopicID which I set as a session var and then call
BuildMainContent()
BuildMainContent() fires which grabs the TopicID from the Session and builds
the maincontent linkbuttons .
The server returns the page to me and the maincontent linkbuttons exist, but
are not wired to their commandevents. Meaning when I click on them nothing
happens.
****************************************
***********
Now if I put the BuildMainContent() in the page_load so it builds in the
postback, everything works okay.
My question is, why aren't the commandevents wired to the linkbuttons, when
I have clearly wired them up in the BuildMainContent() method?
Thanks.
TPS.Hi TPS,
As for the postback issue on the dynamic controls you mentioned, here are
my understandings:
The Controls added in the "BuildMainContent()" method is called in other
controls(built in BuildLeftMenu() in Page_Load)'s post back event ,yes?
That means the maincontent's Linkbutton's postback event handler are wireup
when page processing the postback event rather than before it. So of course
it can 't be fired. And when you move the BuildMainContent() to Page_Load,
their events will be fired.
In fact, as for dynamically created controls, the following two things
should be noticed:
1.Dynamic control should be created everytime page is requested (both the
intial load and the sequential ...). We recommand that we create and add
them in page's OnInit .(in Page_Load is also ok)
2. If the control need to register postback event handler, the handler
should be wireup before the page processing the postback event.
Generally in Page's Init or Load event.
And here is good tech article which has mentioned the things above. I
believe it'll help on undertanding them:
#Understanding ASP.NET View State
http://msdn.microsoft.com/library/d...-us/dnaspp/html
/viewstate.asp
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Steven, thanks for your reply.
I have read for hours and still can't seem to answer my question.
My problem is my LinkButton event handler fires "AFTER" the page has been
built. When the event handler fires, that is where I am getting "TopicID"
from the command arguement. I am using the new TopicID to populate my
LinkButtons, but it is too late because the page has already been built!
Ideas?
Thanks,
TPS
"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:hzkuTgreEHA.720@.cpmsftngxa06.phx.gbl...
> Hi TPS,
> As for the postback issue on the dynamic controls you mentioned, here are
> my understandings:
> The Controls added in the "BuildMainContent()" method is called in other
> controls(built in BuildLeftMenu() in Page_Load)'s post back event ,yes?
> That means the maincontent's Linkbutton's postback event handler are
wireup
> when page processing the postback event rather than before it. So of
course
> it can 't be fired. And when you move the BuildMainContent() to Page_Load,
> their events will be fired.
> In fact, as for dynamically created controls, the following two things
> should be noticed:
> 1.Dynamic control should be created everytime page is requested (both the
> intial load and the sequential ...). We recommand that we create and add
> them in page's OnInit .(in Page_Load is also ok)
> 2. If the control need to register postback event handler, the handler
> should be wireup before the page processing the postback event.
> Generally in Page's Init or Load event.
> And here is good tech article which has mentioned the things above. I
> believe it'll help on undertanding them:
> #Understanding ASP.NET View State
>
http://msdn.microsoft.com/library/d...-us/dnaspp/html
> /viewstate.asp
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
> Get Preview at ASP.NET whidbey
> http://msdn.microsoft.com/asp.net/whidbey/default.aspx
>
Hi TPS,
I've seen for new post discussing on the same problem in this group named
"Can EventHandler fire in Page_PreRender event".
I've posted my reply in the thread. I'd appreciate if you have a look
there.
In addition, if you feel it convenient that we continue to discussing in
one of the two threads, please feel free to followup in either one. Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Trouble with sequence of page build.

Instead of using Hyperlinks and URL / Response.QueryString() parameters to
pass state around in my app, I am using LinkButtons with a corresponding
command event and command arguments.

This page has LinkButtons in a menu down the left hand side, that when
clicked causes the main content of the page to change. The main content of
the page has linkbuttons as well. I am having trouble with the linkbuttons
in the maincontent area keeping their associated command event wired
together.

The linkbuttons in the left menu get built in the page load of the initial
load and all subsequent postbacks (these work fine).

When a left menu linkbutton is clicked it causes a postback,

Page_Load()
BuildLeftMenu() is run in page_load

I don't run BuildMainContent() in the page_load because I don't have the new
TopicID yet.

MyLinkButton_Command() fires next, where I capture the command argument
which tells me the TopicID which I set as a session var and then call
BuildMainContent()

BuildMainContent() fires which grabs the TopicID from the Session and builds
the maincontent linkbuttons .

The server returns the page to me and the maincontent linkbuttons exist, but
are not wired to their commandevents. Meaning when I click on them nothing
happens.

************************************************** *

Now if I put the BuildMainContent() in the page_load so it builds in the
postback, everything works okay.

My question is, why aren't the commandevents wired to the linkbuttons, when
I have clearly wired them up in the BuildMainContent() method?

Thanks.

TPS.Hi TPS,

As for the postback issue on the dynamic controls you mentioned, here are
my understandings:

The Controls added in the "BuildMainContent()" method is called in other
controls(built in BuildLeftMenu() in Page_Load)'s post back event ,yes?

That means the maincontent's Linkbutton's postback event handler are wireup
when page processing the postback event rather than before it. So of course
it can 't be fired. And when you move the BuildMainContent() to Page_Load,
their events will be fired.

In fact, as for dynamically created controls, the following two things
should be noticed:
1.Dynamic control should be created everytime page is requested (both the
intial load and the sequential ...). We recommand that we create and add
them in page's OnInit .(in Page_Load is also ok)

2. If the control need to register postback event handler, the handler
should be wireup before the page processing the postback event.
Generally in Page's Init or Load event.

And here is good tech article which has mentioned the things above. I
believe it'll help on undertanding them:

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/d...-us/dnaspp/html
/viewstate.asp

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Steven, thanks for your reply.

I have read for hours and still can't seem to answer my question.

My problem is my LinkButton event handler fires "AFTER" the page has been
built. When the event handler fires, that is where I am getting "TopicID"
from the command arguement. I am using the new TopicID to populate my
LinkButtons, but it is too late because the page has already been built!

Ideas?

Thanks,
TPS

"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:hzkuTgreEHA.720@.cpmsftngxa06.phx.gbl...
> Hi TPS,
> As for the postback issue on the dynamic controls you mentioned, here are
> my understandings:
> The Controls added in the "BuildMainContent()" method is called in other
> controls(built in BuildLeftMenu() in Page_Load)'s post back event ,yes?
> That means the maincontent's Linkbutton's postback event handler are
wireup
> when page processing the postback event rather than before it. So of
course
> it can 't be fired. And when you move the BuildMainContent() to Page_Load,
> their events will be fired.
> In fact, as for dynamically created controls, the following two things
> should be noticed:
> 1.Dynamic control should be created everytime page is requested (both the
> intial load and the sequential ...). We recommand that we create and add
> them in page's OnInit .(in Page_Load is also ok)
> 2. If the control need to register postback event handler, the handler
> should be wireup before the page processing the postback event.
> Generally in Page's Init or Load event.
> And here is good tech article which has mentioned the things above. I
> believe it'll help on undertanding them:
> #Understanding ASP.NET View State
http://msdn.microsoft.com/library/d...-us/dnaspp/html
> /viewstate.asp
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
> Get Preview at ASP.NET whidbey
> http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi TPS,

I've seen for new post discussing on the same problem in this group named
"Can EventHandler fire in Page_PreRender event".

I've posted my reply in the thread. I'd appreciate if you have a look
there.
In addition, if you feel it convenient that we continue to discussing in
one of the two threads, please feel free to followup in either one. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx