S
Simon
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
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