Error converting data type nvarchar to int.

Hello,
 I have a page to insert data in a table Survey... I keep getting this error:
 Error converting data type nvarchar to int.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to int.

Source Error:

Line 216:        
Line 217:            //int SurveyID = Convert.ToInt32(cmd.ExecuteScalar());
Line 218:            cmd.ExecuteNonQuery();
Line 219:        
Line 220:            myConnection.Close();
see attached the procedure
See below my cs code
protected void Save_Evaluation_CLick(object sender, EventArgs e)
    {
        RadTabStrip1.Tabs[0].Enabled = true;
        RadTabStrip1.Tabs[1].Enabled = true;
        RadTabStrip1.Tabs[2].Enabled = true;
        RadTabStrip1.Tabs[3].Enabled = true;
        RadTabStrip1.Tabs[4].Enabled = true;
        //RadTabStrip1.Tabs[5].Enabled = true;
        RadMultiPage1.SelectedIndex = 0;
        RadTabStrip1.SelectedIndex = 0;
        PageView1.Focus();
        Console.Write("You have succesfully entered the participant information");
 
 
 
        if (!IsEmpty(Request.QueryString["r"]))
        {
            // create the insert command
            //SqlCommand cmd;
            SqlCommand cmd = new SqlCommand("sp_SurveyInsertInfo", myConnection);
            cmd.CommandType = CommandType.StoredProcedure;
 
            cmd.Parameters.Add(new SqlParameter("@ParticipantID", txtParticipantID.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@SchoolID", ddlSchoolName.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@EvaluationVisitID", txtEvalVisitID.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Gender", txtGender.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@SchoolStatus", txtSchoolStatus.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Age", txtAge.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Race_Ethnicity", txtRace.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@PrimaryLanguage", txtPrimLanguage.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@HealthCoverage", rdbHealthCoverage.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@ChildHealthCoverage", rdbChildHealthCoverage.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Pregnant", rdlPregnant.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Months_Weeks_Pregnant", txtMonthsPregnant.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@TimesPregnant", txtPregnantTimes.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@PregnantSincePPT", rdbPregnantSincePPT.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Live_with", ddlLiveWith.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@OtherLive_with", txtOtherLivewith.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Parenting", ddlParenting.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@GoodParent", ddlGoodParent.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Contraception", rblAvoidPreg.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@OtherContraception", txtOtherPregAvoid.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@FuturePlanning", ddlFuturePlanning.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@DecisionMaker", ddlDecisionMaker.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@GraduationImp", ddlGraduationImp.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@College_Training", ddlCollege_Training.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@College_Training_Kinds", ddlCollege_Training_Kinds.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@ParentSupport", ddlParentSupport.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Father_Babe_Rlt", ddlFather_Babe_Rlt.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Satisfaction_NHF", ddlSatisfaction_NHF.SelectedValue));
            cmd.Parameters.Add(new SqlParameter("@Skills_Services", ddlSkills_Services.SelectedValue));
        
                    
        myConnection.Open();
        
            //int SurveyID = Convert.ToInt32(cmd.ExecuteScalar());
            cmd.ExecuteNonQuery();
        
            myConnection.Close();
    }
 
 
 
    }

Open in new window

procedure.txt
RomacaliAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Anthony PerkinsConnect With a Mentor Commented:
>>in the database it is is int for all<<
You are missing my point. I know they are integers in the database.  I am asking if you have verified that the values passed to the stored procedure are in fact integers.

Finally have you added the missing parameter?
0
 
cdaly33Commented:
At the end of your procedure instead of the select statement you have there now try:
SELECT SCOPE_IDENTITY() as SurveyID

Open in new window

0
 
cdaly33Commented:
Sorry, I meant:
SELECT @@SCOPE_IDENTITY() as SurveyID

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
cdaly33Commented:
I'm have trouble tonight because I messed up again.  I meant:

Also have you made sure that it's inserting the rows correctly?
SELECT @@IDENTITY() as SurveyID

Open in new window

0
 
williamcampbellCommented:
SELECT CAST(SCOPE_IDENTITY() AS INT)  as SurveyID

In your procedure Maybe?

What type is SurveyID
0
 
RomacaliAuthor Commented:
still the same error :(
0
 
RomacaliAuthor Commented:
the SurveyID as int
0
 
cdaly33Commented:
Were you able to verify that the row is created in the tbl_Survey table?
0
 
RomacaliAuthor Commented:
nope.. it doesn't create a row on the table :(.. I'm very lost
0
 
cdaly33Commented:
Ok, so let's take a couple steps back.  If the row isn't making it in the table then we need to start there.  The first step would be to verify that you're providing all of the information correctly to the stored procedure.  You need to manually run the stored proc in query analyzer and see if/where its erroring.  From there you'll be able to see what data is missing or not of the correct type which is causing the error.
0
 
RomacaliAuthor Commented:
I tested in the procedure and it worked. Not sure why the cs is not.
0
 
RomacaliAuthor Commented:
Error converting data type nvarchar to int.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to int.

Source Error:

Line 215:        myConnection.Open();
Line 216:        
Line 217:       int SurveyID = Convert.ToInt32(cmd.ExecuteScalar());
Line 218:            cmd.ExecuteNonQuery();
Line 219:        


Source File: c:\WWWroot\nhfca\beta\PPT\SurveyData\evaluationTEST.aspx.cs    Line: 217
0
 
cdaly33Commented:
When you say the procedure worked do you mean that the row was inserted in to the table and the identity was correctly returned?  If so then there must be something in the cs code that isn't assigning the variables the same when you run it from query analyzer.
0
 
Anthony PerkinsCommented:
>>I tested in the procedure and it worked. Not sure why the cs is not.<<
That is because the errror is not coming from your stored procedure.  Here is the reason as posted by cdaly33 #24417533 (no points please):
"The first step would be to verify that you're providing all of the information correctly to the stored procedure."

You are supplying the wrong value to the parameter.  For example, @ParticipantID is integer and txtParticipantID.Text is non-integer.

Unfortunately, until you post your stored procedure sp_SurveyInsertInfo all we can do is make wild guesses as to the exact problem.
0
 
RomacaliAuthor Commented:
not sure what else I can do :(
0
 
Anthony PerkinsCommented:
>>not sure what else I can do<<
Perhaps you overlooked my comment:
"Unfortunately, until you post your stored procedure sp_SurveyInsertInfo all we can do is make wild guesses as to the exact problem."

So perhaps you can post your Stored Procedure.
0
 
cdaly33Commented:
acperkins the procedure was already posted as attachment in the original post
0
 
RomacaliAuthor Commented:
see my procedure attached.
procedure.txt
0
 
Anthony PerkinsCommented:
>>acperkins the procedure was already posted as attachment in the original post<<
You are absolutely right.  What baffles me then is why nobody pointed out that the author is missing a parameter.
0
 
Anthony PerkinsCommented:
>>see my procedure attached.<<

Have you verified that the values for the following are in fact integers?
txtParticipantID.Text                    
txtEvalVisitID.Text                      
txtGender.Text                          
txtAge.Text                              
txtRace.Text                            
txtPrimLanguage.Text                    
txtMonthsPregnant.Text                  
txtPregnantTimes.Text                                        
                           
0
 
RomacaliAuthor Commented:
in the database it is is int for all
for the ParticipantID it is coming from the page  from the other page as in the postback as:
PostBackUrl='<%#"evaluation.aspx?r="+Eval("ParticipantID")+"&"+"e="+Eval("EvaluationVisitID")  %>'
?r=20&e=39

these:
txtGender.Text                          
txtAge.Text                              
txtRace.Text                            
txtPrimLanguage.Text                    
txtMonthsPregnant.Text                  
txtPregnantTimes.Text      
they are numeric
0
 
williamcampbellCommented:
quote "in the database it is is int for all"

      @OtherLive_with as nvarchar(50),
      @OtherContraception as nvarchar(50),

These are two non int fields
0
 
RomacaliAuthor Commented:
on the database the nvarchar of 50 are:
PrimaryLanguage
OtherLive_with
OtherContraception

the problem is they all except NULL but SuveyID
 maybe the problem is that some fields are empty ? when I submit?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.