468,785 Members | 1,665 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

Error inserting a field in sql


HI...

I have a web page that pass aa context to another..
The second page receives the context and need to put the value of the
context into a sql table..

The value is passing ok, 'cause i have a label that print the value, and
it's alright...

I pass the context value to a variable and try to imput it on the table
but the field always receive a 0 value...

heres my code:

int empresa= new int();

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
empresa=Convert.ToInt32(Context.Items["id_emp"].ToString());

Label7.Text=empresa.ToString();

}

// Here the label display the correct value of the "id_emp"
SqlConnection cnn = new
SqlConnection("server=(local);database=varired;tru sted_connection=true")
;
SqlCommand cmm = new SqlCommand("Insert into contatos
(id_cliente,contato,depto,ddd,fone,ramal,email) values
(@a,@b,@c,@d,@e,@f,@g)",cnn);
cmm.Parameters.Add("@a",SqlDbType.Int).Value=empre sa;

//But here it insert the number 0 on the table...
How can this happen???

[]s...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #1
3 1045
Hi,

Since you did not post the complete code of the page, I can only guess. My
guess is that you are inserting into the database on some button click
event, which happens when Page.IspostBack is true, so the code that sets the
field "empresa" is not executed and "empresa" is initialized to 0 (this is
what happens when you call the parameterless constructor of System.Int32).

What you can do, if this is the case, is to hold this value in the viewstate
like this:

public int Empresa
{
get
{
object result = ViewSate["empresa"];
if(result == null)
return 0;
return (int)result;
}
set{ViewState["empresa"] = value;}
}

Just set the property once when IsPostBack is false.

Hope this helps
Martin Dechev
ASP.NET MVP
"Ricardo Luceac" <r_******@hotmail.com> wrote in message
news:uj**************@tk2msftngp13.phx.gbl...

HI...

I have a web page that pass aa context to another..
The second page receives the context and need to put the value of the
context into a sql table..

The value is passing ok, 'cause i have a label that print the value, and
it's alright...

I pass the context value to a variable and try to imput it on the table
but the field always receive a 0 value...

heres my code:

int empresa= new int();

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
empresa=Convert.ToInt32(Context.Items["id_emp"].ToString());

Label7.Text=empresa.ToString();

}

// Here the label display the correct value of the "id_emp"
SqlConnection cnn = new
SqlConnection("server=(local);database=varired;tru sted_connection=true")
;
SqlCommand cmm = new SqlCommand("Insert into contatos
(id_cliente,contato,depto,ddd,fone,ramal,email) values
(@a,@b,@c,@d,@e,@f,@g)",cnn);
cmm.Parameters.Add("@a",SqlDbType.Int).Value=empre sa;

//But here it insert the number 0 on the table...
How can this happen???

[]s...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 19 '05 #2

Thanks... I didn't know that the value of the variable disapear with a
postback...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #3
Yes it does. The web is a little different in that it doesn't keep track of
state variables on post back. Since, on each post back a new class for your
aspx page is generated. So, you have to use either viewstate, session
variables, application variables or some other persistance method to store
your temporary data.

"Ricardo Luceac" wrote:

Thanks... I didn't know that the value of the variable disapear with a
postback...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sean Abrahams | last post: by
1 post views Thread by Paul Mendez | last post: by
6 posts views Thread by BaWork | last post: by
reply views Thread by pere | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.