Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Pawan Awasthi
More...


Forums » .NET » ASP.NET »

How to call sql server function through c#


Posted Date: 18 Jun 2012      Posted By:: bujji     Member Level: Gold    Member Rank: 400     Points: 5   Responses: 5



I am generating reports,
normally we are executing querys or procs by using

conReport.Open();
cmdReport.CommandType = CommandType.Text;
cmdReport.Connection = conReport;
cmdReport.CommandText = "SELECT * FROM tblCredentialUsers";
drReport = cmdReport.ExecuteReader();
dsReport.Tables[0].Load(drReport);
// drReport.Close();
//conReport.Close();
reportViewer1.LocalReport.ReportEmbeddedResource = "ReportsApplication1.Report1.rdlc";
Now I want to call function (sql server) how to call plz.. tell me
my function name is
udf_GetDetailCredentialReport

ASAP..

plz..






Responses

#675984    Author: chidambaram      Member Level: Gold      Member Rank: 333     Date: 18/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,
That s also the same.. refer below

dbcon.con.Open();
cmd = new OdbcCommand("{call proc_verify(?)}", dbcon.con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("uname", Login1.UserName);
reader = cmd.ExecuteReader();


else

cmd = New SqlCommand
sCmdText = "select dbo." & funcName & "("
cmd.CommandType = CommandType.Text
cmd.Connection = _sqlConn
cmd.CommandTimeout = 0



With Regards,
Chidambaram
+91 94898 10101



 
#675987    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 18/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

You can call it just like a normal stored procedure but make sure you use Command.ExecuteScaler()


SqlConnection con = new SqlConnection(connectionString);
SqlCommand com = new SqlCommand("Execute dbo.Funcname", con);
return (Boolean)com.ExecuteScalar();



Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#675991    Author: sudhajosyula      Member Level: Gold      Member Rank: 46     Date: 18/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

See if the user defined function as input parameter to be considering into is as follows:

"select udf_GetDetailCredentialReport('"+ID+"')";

See the below code without input parameters assigned to the user defined function is as follows :


protected void Page_Load(object sender,eventargs e)
{
reportViewer1.LocalReport.ReportEmbeddedResource = "ReportsApplication1.Report1.rdlc";
string strQuery = "select databasename.dbo.udf_GetDetailCredentialReport";
SqlCommand sqlCmd = new SqlCommand(strQuery, conReport);
conReport.Open();
drReport1= sqlCmd.ExecuteReader();
string retString="";
while (dataReport1.Read())
{
retString += dataReport1[0].ToString();
}
dataReport1.Close();
conReport.Dispose();
conReport.Close();






 
#676282    Author: Siva Prasad      Member Level: Gold      Member Rank: 61     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2


SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("dbo.udf_GetDetailCredentialReport", con);
return (Boolean)cmd.ExecuteScalar();



 
#676312    Author: Ultimaterengan        Member Level: Gold      Member Rank: 9     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4


Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim strusername As String = String.Empty
Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
con = New SqlConnection(("server=ServerName;database=DataBaseName;uid=userid;pwd=password;"))
cmd = New SqlCommand("select count(*) from Tablename where Username='" & TextBox1.Text & "'", con)
con.Open()
strusername = cmd.ExecuteScalar
con.Close()
If strusername <> "0" Then
MsgBox("UserName Already Exists")
' Response.Redirect("Default2.aspx")
End If
End Sub
End Class




Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!


http://renganathan1984.blogspot.com/



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How you implemented Reflection in your project?
Previous : I have used updatepanel and WCF service in my asp.net and but my application is very slo
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.