473,804 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use Stored Procedure in C# Web Part

I have a problem using a stored procedure in C#. Im using SQL2000 and
C# with web parts.

Currently i put the entire sql query in the CommandText. Then it works
correctly and gives the companynames in my column. As u can see below
in the C# code.

I made a Stored Procedure from the query and want to call that instead,
it saves a lot of code in my project.

What lines do i have to change, and how, to make this Stored Procedure
work and give the same output as my sql string in the code does now.

=======C# code with long SQL query in it========

protected override void CreateChildCont rols()
{

tbSearchC = new TextBox();
btnSearchC = new Button();
btnSearchC.Text = "Find";
btnSearchC.Clic k += new EventHandler(th is.btnSearchC_S earchAction );
Controls.Add(tb SearchC);
Controls.Add(bt nSearchC);
dgDataC_search = new DataGrid();
dgDataC_search. HeaderStyle.Bor derWidth = 2;
dgDataC_search. HeaderStyle.Fon t.Bold = true;
dgDataC_search. HeaderStyle.Bac kColor = Color.LightStee lBlue;
dgDataC_search. HeaderStyle.For eColor = Color.Black;
dgDataC_search. HeaderStyle.Wra p = false;
dgDataC_search. GridLines = System.Web.UI.W ebControls.Grid Lines.Both;
dgDataC_search. SelectedItemSty le.BackColor =
System.Drawing. Color.YellowGre en;
dgDataC_search. AutoGenerateCol umns = false;
BoundColumn newColumn_Compa nyname;
newColumn_Compa nyname = new BoundColumn();
newColumn_Compa nyname.DataFiel d = "name";
newColumn_Compa nyname.HeaderTe xt = "Companynam e:";
newColumn_Compa nyname.HeaderSt yle.Width=120;
dgDataC_search. Columns.Add(new Column_Companyn ame);

Controls.Add(dg DataC_search);
}

private void btnSearchC_Sear chAction(object sender, System.EventArg s e)
{

string sqlString ="Persist Security Info=False;Inte grated
Security=SSPI;i nitial catalog=SIX;ser ver=(local)";
SqlConnection sqlCon = new SqlConnection(s qlString);
sqlCon.Open();
SqlCommand sc_findC = new SqlCommand();
sc_findC.Connec tion = sqlCon;
sc_findC.Comman dType = CommandType.Tex t;

sc_findC.Comman dText ="select contact.contact _id, contact.[name],
isnull(address. city, '') as city, isnull(person.m rmrs, '') as mrmrs,
isnull(person.i nitials, '') as initials, isnull(person.t itle, '' ) as
title, isnull(person.f irstname, '') as firstname,
isnull(person.l astname, '') as lastname, isnull(phone.ph one, '') as
phonenr, isnull(phone1.p hone, '') as ext, isnull(phone2.p hone, '') as
mobilenr, isnull(email.em ail_address, '') as emailaddress from
crm5.contact contact left join crm5.address address on
contact.contact _id = address.owner_i d and address.atype_i dx = 2 left
join crm5.person person on contact.contact _id = person.contact_ id left
join crm5.phone phone on contact.contact _id = phone.owner_id and
phone.ptype_idx = 1 left join crm5.phone phone1 on person.person_i d =
phone1.owner_id and phone1.ptype_id x = 16385 left join crm5.phone
phone2 on person.person_i d = phone2.owner_id and phone2.ptype_id x =
16389 left join crm5.email email on person.person_i d = email.person_id
and email.rank = 1 where contact.[name] like '%"+tbSearchC.T ext+"%'
order by contact.[name], firstname, lastname";
DataSet ds_findC = new DataSet();
SqlDataAdapter AdapterC = new SqlDataAdapter( sc_findC);
AdapterC.Fill(d s_findC);
dgDataC_search. DataSource = ds_findC;
dgDataC_search. DataBind();
}
}
}

====The Stored pdocedure i want to use in my C# to eliminate the long
sql query in the code===

CREATE PROCEDURE SearchCompany
@CompanyName varchar(50)
AS

select contact.contact _id,
contact.[name],
isnull(address. city, '') as city,
isnull(person.m rmrs, '') as mrmrs,
isnull(person.i nitials, '') as initials,
isnull(person.t itle, '' ) as title,
isnull(person.f irstname, '') as firstname,
isnull(person.l astname, '') as lastname,
isnull(phone.ph one, '') as phonenr,
isnull(phone1.p hone, '') as ext,
isnull(phone2.p hone, '') as mobilenr,
isnull(email.em ail_address, '') as emailaddress
from crm5.contact contact
left join crm5.address address on contact.contact _id =
address.owner_i d and address.atype_i dx = 2
left join crm5.person person on contact.contact _id = person.contact_ id
left join crm5.phone phone on contact.contact _id = phone.owner_id and
phone.ptype_idx = 1
left join crm5.phone phone1 on person.person_i d = phone1.owner_id and
phone1.ptype_id x = 16385
left join crm5.phone phone2 on person.person_i d = phone2.owner_id and
phone2.ptype_id x = 16389
left join crm5.email email on person.person_i d = email.person_id and
email.rank = 1
where contact.[name] like '%' + @CompanyName + '%'
order by contact.[name], firstname, lastname
GO

Mar 6 '06 #1
3 2168
On 6 Mar 2006 05:40:19 -0800, "Simon" <s.*******@gmai l.com> wrote:
I have a problem using a stored procedure in C#. Im using SQL2000 and
C# with web parts.

Currently i put the entire sql query in the CommandText. Then it works
correctly and gives the companynames in my column. As u can see below
in the C# code.

I made a Stored Procedure from the query and want to call that instead,
it saves a lot of code in my project.

What lines do i have to change, and how, to make this Stored Procedure
work and give the same output as my sql string in the code does now.

=======C# code with long SQL query in it========
[code]}

====The Stored pdocedure i want to use in my C# to eliminate the long
sql query in the code===

[STORED PROC]

Simon,

First you create a command: (where con = an open connection)

SqlCommand oCommand = new SqlCommand(Comm andName, con);
oCommand.Comman dType = CommandType.Sto redProcedure;

Then you throw in your parameter:

SqlParameter NewParam = Command.Paramet ers.Add("@Compa nyName",
SqlDbType.VarCh ar, 50, Value)
NewParam.Value = "%"+tbSearchC.T ext+"%";

Then you select the output:
SqlDataAdapter adap = new SqlDataAdapter( );
adap.SelectComm and = oCommand;
DataSet set = new DataSet("return ");
adap.Fill(set);
oCommand.Connec tion.Close();

Your dataset returned is your "ds_findC"

Leon

Mar 6 '06 #2
Ok i followed your directions and came up with the following, i got 2
minor errors on one line:
private void btnZoekC_Zoekop dracht(object sender, System.EventArg s e)
{
string sqlString ="Persist Security Info=False;Inte grated
Security=SSPI;i nitial catalog=SIX;ser ver=(local)";
SqlConnection sqlCon = new SqlConnection(s qlString);
sqlCon.Open();

SqlCommand sc_findC = new SqlCommand("Sea rchCompany", sqlCon); //is
the input SearchCompany ok here, didnt realy understand what u mean
with CommandName.

sc_findC.Comman dType = CommandType.Sto redProcedure;

SqlParameter NewParam =
Command.Paramet ers.Add("@Compa nyName", SqlDbType.VarCh ar, 50, Value);
//I got the following errors on this line: The type or namespace name
'Command' could not be found (are you missing a using directive or an
assembly reference?)
// And: The name 'Value' does not exist in the class or namespace
'ZoekContact.We bPart1'
//Which assembly reference do i have to add for the 'Command' and
'Value' message the disappear.

NewParam.Value = "%"+tbSearchC.T ext+"%";

SqlDataAdapter AdapterC = new SqlDataAdapter( );
AdapterC.Select Command = sc_findC;
DataSet ds_findC= new DataSet("return ");
AdapterC.Fill(d s_findC);
sc_findC.Connec tion.Close();
}

Mar 7 '06 #3
Tnx man i got it working now, cause of your great directions!

tnx

Mar 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
28272
by: Eric Martin | last post by:
Hello, Does anyone know of a way to loop thru a SQL table using code in a stored procedure? I need to go thru each record in a small table and build a string using values from the fields associated with a part number, and I can't find any way to process each record individually. The string needs to be initialized with the data associated with the 1st record's part number, and I need to build the string until a new part number is...
4
6235
by: deprins | last post by:
Hello, I have wrote a stored procedure but its real slow. Its activated by a button on web page but its takes to long to process and the web server gives a timeout message after 5 minutes. Is there anyway to speed up this stored procedure? What am I doing wrong here? ------------------------------------------------------------------------------
15
6017
by: Jarrod Morrison | last post by:
Hi All Im generally a vb programmer and am used to referencing multiple records returned from a query performed on an sql database and im trying to move some functions of my software into sql stored procedures. So far ive been able to move the functions relatively easily but im unsure about how to output multiple values from an sql stored procedure. By this i mean for example one of the stored procedures may take your username and return...
4
8102
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure creates a string of custom SQL statement and returns this string back to the main stored procedure. This SQL statements work fine on there own. The SQL returned from the sub stored procedure are returned fine. The datatype of the variable that...
4
7191
by: John | last post by:
Hi everyone, I have a stored procedure which I use to query a table. The first part of the stored procedure uses a cursor to update a temp table whilst the second part of the query actually retrieves information from a database table utilising information based on the temp table. My problem is that when I run the procedure, the cursors status is output and therefore becomes part of the result set. I really only want the information...
3
2486
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm satified with the table structure. I've moved on to building some front ends for our users. I'm running into situations where I want subreports to be built from queries that are dependent on the values in other controls. I've played with stored...
3
6875
by: comp_databases_ms-sqlserver | last post by:
This post is related to SQL server 2000 and SQL Server 2005 all editions. Many of my stored procedures create temporary tables in the code. I want to find a way to find the query plan for these procs Repro --*********************************** use pubs go CREATE PROCEDURE Test @percentage int
1
7532
by: peaceburn | last post by:
Hi, I'm gonna pull my hair in the coming days with these DB2 stored procedures. So the issue, let's assume a simple stored procedure like this : CREATE PROCEDURE MYSCHEMA.PROCEDURE1 ( ) DYNAMIC RESULT SETS 1 ------------------------------------------------------------------------
7
16413
by: mkarbarz | last post by:
Hello, I need to come up with a stored procedure that will allow me to read data from another database. The database I need to read the data from is a UniData residing on a Unix server. The stored procedure needs to reside on my sql 2005 server. The task is very simple in Access as we have ODBC connections set up to the UniData via Informix (or IBM) UniData ODBC drivers. I can easily combine my UniData and Sql Server tables from...
2
5656
by: IuliaS | last post by:
Hello everyone! I want to create a stored procedure, so I can more easily, and transparent retrieve data from db2. Long story short: when a user wants to put some data in the DB, he also creates the tables and their links. When getting the data from the DB... well suffice to say it's ugly. I want to use one stored procedure that will return a result set as (name, value) pairs so I can display it nice and easy in the UI. So far I've managed to...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.