473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1153
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean Abrahams | last post by:
The following is a reprint of a message I sent to the tutor list a long time ago, that I haven't gotten around to discussing with anyone else and failed to hear a reply on the tutor list. Hoping...
5
by: F. Barth | last post by:
Hello, I've posted this problem to one other newsgroups, and gotten some diagnostic help, but I still need a solution. The full text of the message box is: "The field is too small to accept the...
1
by: Paul Mendez | last post by:
Whenever I first try to enter data into my form, it prompts me with this following error: "The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less...
14
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
1
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO...
6
by: BaWork | last post by:
I have the following to insert a new record: <% .. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "DSN=qqqqq;" SQLStmt = "INSERT INTO tbl_qqqqqq (main_cat, cat_fee, fee_amount)...
7
by: iritchie | last post by:
Hello all, I am trying to write a script which breaks down a single address field into individual fields, with char(10) or a carriage return as the delimiter. "empltable" is the table I am...
0
by: pere | last post by:
hi everybody, I have a script that inserts .csv data into a table; I want different users to insert their data in the same table. To differenciate them I make them fill a name (called "nom")...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.