473,386 Members | 1,810 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,386 software developers and data experts.

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 CreateChildControls()
{

tbSearchC = new TextBox();
btnSearchC = new Button();
btnSearchC.Text = "Find";
btnSearchC.Click += new EventHandler(this.btnSearchC_SearchAction );
Controls.Add(tbSearchC);
Controls.Add(btnSearchC);
dgDataC_search = new DataGrid();
dgDataC_search.HeaderStyle.BorderWidth = 2;
dgDataC_search.HeaderStyle.Font.Bold = true;
dgDataC_search.HeaderStyle.BackColor = Color.LightSteelBlue;
dgDataC_search.HeaderStyle.ForeColor = Color.Black;
dgDataC_search.HeaderStyle.Wrap = false;
dgDataC_search.GridLines = System.Web.UI.WebControls.GridLines.Both;
dgDataC_search.SelectedItemStyle.BackColor =
System.Drawing.Color.YellowGreen;
dgDataC_search.AutoGenerateColumns = false;
BoundColumn newColumn_Companyname;
newColumn_Companyname = new BoundColumn();
newColumn_Companyname.DataField = "name";
newColumn_Companyname.HeaderText = "Companyname:";
newColumn_Companyname.HeaderStyle.Width=120;
dgDataC_search.Columns.Add(newColumn_Companyname);

Controls.Add(dgDataC_search);
}

private void btnSearchC_SearchAction(object sender, System.EventArgs e)
{

string sqlString ="Persist Security Info=False;Integrated
Security=SSPI;initial catalog=SIX;server=(local)";
SqlConnection sqlCon = new SqlConnection(sqlString);
sqlCon.Open();
SqlCommand sc_findC = new SqlCommand();
sc_findC.Connection = sqlCon;
sc_findC.CommandType = CommandType.Text;

sc_findC.CommandText ="select contact.contact_id, contact.[name],
isnull(address.city, '') as city, isnull(person.mrmrs, '') as mrmrs,
isnull(person.initials, '') as initials, isnull(person.title, '' ) as
title, isnull(person.firstname, '') as firstname,
isnull(person.lastname, '') as lastname, isnull(phone.phone, '') as
phonenr, isnull(phone1.phone, '') as ext, isnull(phone2.phone, '') as
mobilenr, isnull(email.email_address, '') as emailaddress from
crm5.contact contact left join crm5.address address on
contact.contact_id = address.owner_id and address.atype_idx = 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_id =
phone1.owner_id and phone1.ptype_idx = 16385 left join crm5.phone
phone2 on person.person_id = phone2.owner_id and phone2.ptype_idx =
16389 left join crm5.email email on person.person_id = email.person_id
and email.rank = 1 where contact.[name] like '%"+tbSearchC.Text+"%'
order by contact.[name], firstname, lastname";
DataSet ds_findC = new DataSet();
SqlDataAdapter AdapterC = new SqlDataAdapter(sc_findC);
AdapterC.Fill(ds_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.mrmrs, '') as mrmrs,
isnull(person.initials, '') as initials,
isnull(person.title, '' ) as title,
isnull(person.firstname, '') as firstname,
isnull(person.lastname, '') as lastname,
isnull(phone.phone, '') as phonenr,
isnull(phone1.phone, '') as ext,
isnull(phone2.phone, '') as mobilenr,
isnull(email.email_address, '') as emailaddress
from crm5.contact contact
left join crm5.address address on contact.contact_id =
address.owner_id and address.atype_idx = 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_id = phone1.owner_id and
phone1.ptype_idx = 16385
left join crm5.phone phone2 on person.person_id = phone2.owner_id and
phone2.ptype_idx = 16389
left join crm5.email email on person.person_id = email.person_id and
email.rank = 1
where contact.[name] like '%' + @CompanyName + '%'
order by contact.[name], firstname, lastname
GO

Mar 6 '06 #1
3 2152
On 6 Mar 2006 05:40:19 -0800, "Simon" <s.*******@gmail.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(CommandName, con);
oCommand.CommandType = CommandType.StoredProcedure;

Then you throw in your parameter:

SqlParameter NewParam = Command.Parameters.Add("@CompanyName",
SqlDbType.VarChar, 50, Value)
NewParam.Value = "%"+tbSearchC.Text+"%";

Then you select the output:
SqlDataAdapter adap = new SqlDataAdapter();
adap.SelectCommand = oCommand;
DataSet set = new DataSet("return");
adap.Fill(set);
oCommand.Connection.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_Zoekopdracht(object sender, System.EventArgs e)
{
string sqlString ="Persist Security Info=False;Integrated
Security=SSPI;initial catalog=SIX;server=(local)";
SqlConnection sqlCon = new SqlConnection(sqlString);
sqlCon.Open();

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

sc_findC.CommandType = CommandType.StoredProcedure;

SqlParameter NewParam =
Command.Parameters.Add("@CompanyName", SqlDbType.VarChar, 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.WebPart1'
//Which assembly reference do i have to add for the 'Command' and
'Value' message the disappear.

NewParam.Value = "%"+tbSearchC.Text+"%";

SqlDataAdapter AdapterC = new SqlDataAdapter();
AdapterC.SelectCommand = sc_findC;
DataSet ds_findC= new DataSet("return");
AdapterC.Fill(ds_findC);
sc_findC.Connection.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
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...
4
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...
15
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...
4
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...
4
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...
3
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...
3
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...
1
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 ( )...
7
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...
2
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...
0
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...
0
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,...
0
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...

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.