This is a migrated thread and some comments may be shown as answers.

insert entity to database using sql stored procedure:Urgent

1 Answer 58 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Derya Altuntas
Top achievements
Rank 1
Derya Altuntas asked on 20 Nov 2009, 12:31 PM
I want to add a new entity to table which has relation to another table.I write an sql procedure to do this.But there is a coulum ın the table that in nvarchar.My stored procedure includes this.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_ins_Object_DIS] (@object_ID UNIQUEIDENTIFIER ,@category INTEGER , @country INTEGER , @domain INTEGER , @extra INTEGER , @kind INTEGER , @marking NVARCHAR , @specific INTEGER , @sub_category INTEGER) AS INSERT INTO [Objects_DIS] ( [object_ID] , [category] , [country] , [domain] , [extra] , [kind] , [marking] , [specific], [sub_category] ) VALUES ( @object_ID , @category , @country , @domain , @extra , @kind , @marking , @specific , @sub_category)


I am calling this procedure from code by sending parameter but there is no equivalent for nvarchar in C#.ı want to sent textbox string value.It gives error.Please help.

if (!m_scope.Transaction.IsActive)
            {
                m_scope.Transaction.Begin();
            }
            Platform selectedPlatform = (Platform)m_lbx_SAMPlatform.SelectedItem;


            string query = "SELECT * FROM ObjectsDISExtent AS c WHERE c.platform = $1";

            IQueryResult result = m_scope.GetOqlQuery(query).Execute(selectedPlatform);

            ArrayList resultSet = result.ToList();

            if (resultSet.Count > 0)//to change existing variables
            {

                ObjectsDIS platformDIS = (ObjectsDIS)resultSet[0];
                platformDIS.Kind = m_cmbbx_SamKind.SelectedIndex;
                platformDIS.Domain = m_cmbbx_SamDomain.SelectedIndex;
                platformDIS.Country = Int16.Parse(m_txtbx_SamCountry.Text);
                platformDIS.Category = Int16.Parse(m_txtbx_SamCategory.Text);
                platformDIS.Specific = Int16.Parse(m_txtbx_SAMSpecific.Text);
                platformDIS.SubCategory = Int16.Parse(m_txtbx_SAMSubCategory.Text);
                platformDIS.Extra = Int16.Parse(m_txtbx_SamExtra.Text);
                platformDIS.Marking = m_txtbx_SAMMarking.Text;
            }
            else//insert new if there is no entity for selected platform.This gives error
            {
                IQuery newquery = m_scope.GetSqlQuery("sp_ins_Object_DIS ?,?,?,?,?,?,?,?,?",

                                                 null,

                                         "GUID object_ID, INTEGER category, INTEGER country, INTEGER domain ,INTEGER extra, INTEGER kind, NVARCHAR marking,INTEGER specific,INTEGER sub_category");



                IQueryResult newa = newquery.Execute(new object[] { selectedPlatform.Id, Int16.Parse(m_txtbx_SamCategory.Text), Int16.Parse(m_txtbx_SamCountry.Text), m_cmbbx_SamDomain.SelectedIndex, Int16.Parse(m_txtbx_SamExtra.Text), m_cmbbx_SamKind.SelectedIndex, m_txtbx_SAMMarking.Text, Int16.Parse(m_txtbx_SAMSpecific.Text), Int16.Parse(m_txtbx_SAMSubCategory.Text) });
                int newcount = newa.Count;

            }

            m_scope.Transaction.Commit();

1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 25 Nov 2009, 05:24 PM
Hi Derya Altuntas,

Can you please try to change the string that declares the parameters needed by the stored procedure to look like this:
IQuery newquery = m_scope.GetSqlQuery("sp_ins_Object_DIS ?,?,?,?,?,?,?,?,?", null,
                                        "GUID object_ID, INTEGER category, INTEGER country, INTEGER domain ,INTEGER extra, INTEGER kind, VARCHAR marking,INTEGER specific,INTEGER sub_category");
As you can see I have changed the NVARCHAR declaration to VARCHAR one (note that the marking column is still NVARCHAR on the database side. This should solve your problem.

Sincerely yours,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Databases and Data Types
Asked by
Derya Altuntas
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or