I have made a web-service which'd give me the User Information.
[WebMethod]
public void Get_User_Info(string uid)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["contest"].ConnectionString);
SqlCommand com = new SqlCommand("select * from mtblUser where UserId=#UserId", con);
com.CommandType = CommandType.Text;
com.Parameters.Add("#UserId", SqlDbType.NVarChar, 50).Value = uid;
SqlDataAdapter sda = new SqlDataAdapter(com);
DataTable dt = new DataTable();
sda.Fill(dt);
}
this web service is having 1 method Get_User_Info(). but when I try to use this method the namespane show 4 methods like below
Get_User_InfoRequest
Get_User_InfoRequestBody
Get_User_InfoResponse
Get_User_InfoResponseBody
how can I use my method please help.
Try
YourService.ServiceSoapClient _CurrentSrv = new YourService.ServiceSoapClient();
_CurrentSrv.Get_User_Info(YourId);
Related
I have written a webservice to access my SQL database.
[WebMethod]
public void getRoomByBuildingID(int buildingID)
{
string cs = ConfigurationManager.ConnectionStrings["vetDatabase_Wizard"].ConnectionString;
List<Room> rooms = new List<Room>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetRoomsByBuildingID", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter()
{
ParameterName = "#buildingID",
Value = buildingID
};
cmd.Parameters.Add(param);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Room room = new Room();
room.ID = Convert.ToInt32(rdr["ID"]);
room.Name = rdr["Room"].ToString();
room.buildingID = Convert.ToInt32(rdr["Building"]);
rooms.Add(room);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
//Serialize list object into a JSON array and write in into the response stream
Context.Response.Write(js.Serialize(rooms));
}
I keep getting an indexOutOfRangeException at
room.building = Convert.ToInt32(rdr["Building"]);
I've been looking at my sql table, and the column names and types are correct (i.e. ID, Room, Building). I am totally clueless at this point and would greatly appreciate the community's feedback. Thank you!
EDIT:
ALTER PROCEDURE spGetRoomsByBuildingID
#buildingID int
AS
BEGIN
SELECT ID, Room, Building FROM tblRooms
WHERE Building = #buildingID
END
EXEC spGetRoomsByBuildingID
I'm creating a new webservice in ASP.NET 5 using the new .NET Core library, so far I've only hit an issue with using DataSet and DataTable.
According to this site they are not included at this moment in time, which is fine, but I don't know what alternatives I have at this time, so I'm just looking for some guidance.
I have the following code:
public string Get(string p_sUserId, string p_sUserPassword, int p_iCustId)
{
Select qrySelect = new Select();
using (SqlConnection conn = new SqlConnection(Startup.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(qrySelect.getData(), conn))
{
cmd.Parameters.AddWithValue("#Id", sTestId);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
// foo
// bar
}
}
}
}
return "value";
}
How should I handle the data that is being return from the query? I need to build and return a string using the above data fetched from the query. Any help and guidance would be appreciated.
I believe SqlDataReader should work.
string sql = "SELECT * FROM Table";
using (SqlConnection con = new SqlConnection(Startup.ConnectionString)) {
con.Open();
using (SqlCommand command = new SqlCommand(sql, con)) {
using (IDataReader dr = command.ExecuteReader()) {
while (dr.Read()) {
//process data
}
}
}
}
DataTable and SqlDBAdapter are now supported using .NET Standard 2.0. Upgrade to VS2017 Preview, add System.Data.Common and System.Data.SqlClient nugets, and the code below should work. More detail at the blog post here -> https://blogs.msdn.microsoft.com/devfish/2017/05/15/exploring-datatable-and-sqldbadapter-in-asp-net-core-2-0/ . GitHub repo here -> https://github.com/jhealy/aspdotnetcore/tree/master/SqlClientPlay20 .
public static DataTable ExecuteDataTableSqlDA(SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
System.Data.DataTable dt = new DataTable();
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmdText, conn);
da.Fill(dt);
return dt;
}
I have a very unique problem here...
If I'm going to select data from my database, my datatable will not get all of the database content using a SqlDataReader.
DataTable DTReader = new DataTable();
string Query = "SELECT CardCode FROM OCRD";
using (SqlConnection Connection = new SqlConnection(ConnString))
{
Stopwatch watch = new Stopwatch();
watch.Start();
Connection.Open();
using (SqlCommand Command= new SqlCommand(Query, Connection))
{
using (SqlDataReader reader = Command.ExecuteReader())
{
DTReader.Load(reader);
}
}
Connection.Close();
watch.Stop();
MessageBox.Show(watch.Elapsed.ToString());
gridControl1.DataSource = DTReader;
}
In OCRD we have some specific userdata where the cardcode is an unique identifier.
CardCode could be D40000, D410000,... and so on.
We definitely have D46000 codes but my datatable will not fetch them. From D40000 to D44999 we can get the data but we are unable to get data above.
The query is definitely correct...
Does anyone has any ideas regarding this issue ?
Thanks in advance.
I have written code for displaying content of a sql query on a webpage in aspx, I need to edit the column names of the returned result on the webpage. Below is the code for displaying content
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace XYZ
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["ABC"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
string queryString = "select * from asde";
SqlCommand cmd = new SqlCommand(queryString,con);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
SqlDataReader reader;
reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
}
}
}
}
}
any pointers in editing the column names is highly appreciated
You cannot set the datareader as the grid data source. Also data adapter should initialize with the sql command associated.
You can do something like below
SqlCommand cmd = new SqlCommand("select * from asde", con);
cmd.CommandType = CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapater(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
or
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
I am new to WCF. I have done an application which is as follows
I am having Service as follows
void IService1.getAllEmpName()
{
SqlConnection con = new SqlConnection("Data Source=SYSTEM19\\SQLEXPRESS;Initial Catalog=Dora;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select *from Users", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
}
My interface is as follows
[ServiceContract]
public interface IService1
{
[OperationContract]
void getAllEmpName();
[OperationContract]
void editEmployee();
}
In my web page I am doing as follows
private void get_categories()
{
ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client();
GridView1.DataSource = ws.getAllEmpName();
GridView1.DataBind();
}
I am getting error as Cannot convert method group 'getAllEmpName' to non-delegate type 'object'. Did you intend to invoke the method? can any one help
The first problem I see is that your getAllEmpName() method is void. It returns nothing. It will send no data back to the client.
Passing a DataSet through WCF isn't always the best idea either. A single DataTable would be slightly better, but returning a List<> or array would be best. However, try something like:
// Service
DataTable IService1.getAllEmpName()
{
SqlConnection con = new SqlConnection("Data Source=SYSTEM19\\SQLEXPRESS;Initial Catalog=Dora;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select *from Users", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dt.Fill(dt);
return dt;
}
[ServiceContract]
public interface IService1
{
[OperationContract]
DataTable getAllEmpName();
[OperationContract]
void editEmployee();
}
// Client
private void get_categories()
{
ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client();
DataTable data = ws.getAllEmpName();
GridView1.DataSource = data;
GridView1.DataBind();
}
I also came back and re-read this, and noticed that you aren't disposing your WCF client. That is bad! When WCF clients aren't properly aborted or closed they can continue to consume resources, and will hold open the connection until it gets garbage collected. There are plenty of other discussions out there on the topic that you can search for.
Since ClientBase implements IDisposable, you should explicitly dispose of it. Something like:
using(ServiceReference1.Service1Client ws = new ServiceReference1.Service1Client())
{
try
{
// use the "ws" object...
}
finally
{
if(ws.State == CommunicationState.Faulted)
ws.Abort();
else
ws.Close();
}
}