I am using WCF service to display data into gridview but it not properly displaying(Columns are displaying out of order)
Following is my seperate class file containing properties to be added to IList object
Code:
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ServiceTest
{
public class Class1
{
public int index { get; set; }
public string name { get; set; }
public int id { get; set; }
}
}
This is Service interface
IService1.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections;
namespace ServiceTest
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
IList<Class1> GetD();
}
}
This is service class that implements IService.cs
Service1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections;
namespace ServiceTest
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public IList<Class1> GetD()
{
IList<Class1> lst = new List<Class1>();
for (int i = 0; i < 50; i++)
{
Class1 c = new Class1();
c.index = i;
c.name = "Madhavi " + i;
c.id = i + 1;
lst.Add(c);
}
return lst;
}
}
}
Below is my consumer code having one gridview databound control.
And consumer code
protected void Page_Load(object sender, EventArgs e)
{
//IList i = new ArrayList();
Service1Client s = new Service1Client();
// i = s.GetD();
GridView1.DataSource = s.GetD();
GridView1.DataBind();
}
Sounds as if you have AutoGenerateColumns set to true for the GridView1. You can instead try explicitly specifying the columns in the .aspx file in the order that you would like these columns to appear and bind each column with the appropriate field from the Datasource.
<asp:GridView runat="server" id="GridView1">
<Columns>
</Columns>
</GridView>
Related
I've been creating an application for university coursework, using Visual Studio Forms, and I was trying to launch the form I've made to check it's connecting to the database behind it, but it won't accept the name of the form as legitimate. I have no idea why, as it is appearing in the Solution Explorer.
This is the code for the program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FoxhillCustomer
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmViewEditStaff());
}
}
}
This is the code for the frmViewEditStaff.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FoxhillViewEditStaff
{
public partial class frmViewEditStaff : Form
{
SqlDataAdapter daViewEditStaff;
DataSet dsFoxhillDentistryFinal = new DataSet();
SqlCommandBuilder cmdBViewEditStaff;
DataRow drViewEditStaff;
String connStr, sqlViewEditStaff;
int selectedTab = 0;
bool staffSelected = false;
int staffNoSelected = 0;
public frmViewEditStaff()
{
InitializeComponent();
}
private void frmViewEditStaff_Load(object sender, EventArgs e)
{
connStr = #"Data Source = .\sqlexpress; Initial Catalog = InTheDogHouse; Integrated Security = true";
sqlViewEditStaff = #"select * from Staff";
daViewEditStaff = new SqlDataAdapter(sqlViewEditStaff, connStr);
cmdBViewEditStaff = new SqlCommandBuilder(daViewEditStaff);
daViewEditStaff.FillSchema(dsFoxhillDentistryFinal, SchemaType.Source, "ViewEditStaff");
daViewEditStaff.Fill(dsFoxhillDentistryFinal, "ViewEditStaff");
dgvStaff.DataSource = dsFoxhillDentistryFinal.Tables["Staff"];
dgvStaff.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
tabStaff.SelectedIndex = 1;
tabStaff.SelectedIndex = 0;
}
}
}
Your form is in a namespace of FoxhillViewEditStaff. Your Program class is in a namespace of FoxhillCustomer, and you don't have a using directive for the FoxhillViewEditStaff namespace.
I'd strongly advise you to:
Use a single namespace for all the classes in your project
Rename all your forms to follow normal .NET naming conventions, to make the code clearer
You could just add a using directive, but it would be more sensible to just put the classes into the same namespace, IMO.
Whenever I am using Window Forms It works Fine But it always give error with console applicion.
Error- The socket connection was aborted. This could be caused by an
error processing your message or a receive timeout being exceeded by
the remote host, or an underlying network resource issue. Local socket
timeout was '00:01:00'.
Here is My Code
Contract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ClassLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReportService" in both code and config file together.
[ServiceContract(CallbackContract=typeof(IReportServiceCallbak))]
public interface IReportService
{
[OperationContract(IsOneWay=true)]
void ProcessReport();
}
public interface IReportServiceCallbak
{
[OperationContract(IsOneWay=true)]
void Progress(int percentage);
}
}
Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace ClassLibrary1
{
public class ReportService : IReportService
{
public void ProcessReport()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
OperationContext.Current.GetCallbackChannel<IReportServiceCallbak>().Progress(i);
}
}
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Threading;
namespace DuplexClientsss
{
class Program
{
static void Main(string[] args)
{
new Tests();
} }
class Tests : ReportService.IReportServiceCallback
{
ReportService.ReportServiceClient obj;
public Tests()
{
obj = new ReportService.ReportServiceClient(new InstanceContext(this));
obj.ProcessReport();
}
public void Progress(int percentage)
{
Console.WriteLine(percentage);
}
}
}
new Task
(
()=>
{
obj.ProcessReport();
}
).Start();
I want to call the method from the codebehind of a window that is NOT the MainWindow in my WPF application, casting the window type as I do it.
ClientCallBack.cs:
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ChatClient
{
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
public void GetMessage(string message, string userName)
{
//get casted instance of chat client window (NOT MainWindow!)
}
}
}
ChatWPFClient.xaml.cs:
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ChatClient
{
/// <summary>
/// Interaction logic for ChatWPFClient.xaml
/// </summary>
public partial class ChatWPFClient : Window
{
public static IChattingService Server;
private static DuplexChannelFactory<IChattingService> _channelFactory;
public ChatWPFClient()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(), "ChattingServiceEndpoint");
Server = _channelFactory.CreateChannel();
}
private void sendMessage(object sender, RoutedEventArgs e)
{
MessageBox.Show("Not available yet!");
}
public void TakeMessage(string message, string userName)
{
chatBox.Text += userName + ": " + message + "\n";
}
}
}
How can I call the TakeMessage of this method in the other class so I can use that codebehind window to populate the XAML file for ChatWPFClient.xaml? Thanks in advance!
First create an interface that you can pass to the ClientCallback
public interface IMessageHandler
{
void TakeMessage(string message, string userName);
}
Then in the ClientCallBack take the interface as a parameter in the constructor.
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
private IMessageHandler messageHandler;
public ClientCallBack(IMessageHandler messageHandler)
{
this.messageHandler = messageHandler;
}
public void GetMessage(string message, string userName)
{
messageHandler.TakeMessage(message, userName);
}
}
Use the interface for the ChatWpfClient and pass the instance in the constructor.
public partial class ChatWPFClient : Window, IMessageHandler
{
...
public ChatWPFClient()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(this), "ChattingServiceEndpoint");
Server = _channelFactory.CreateChannel();
}
...
// This is a part of the interface now and needs to be implemented here
public void TakeMessage(string message, string userName)
{
chatBox.Text += userName + ": " + message + "\n";
}
}
Also you could just implement the IClient on your ChatWPFClient class and decorate with the CallBackBehavior attribute and just pass itself as the callback. But don't think this is recommended, seems weird.
_channelFactory = new DuplexChannelFactory<IChattingService>(this, "ChattingServiceEndpoint");
I have two solutions TranferService and Sender. TranferService has WCF service and IISHost to host that service. In Sender solution i have windows forms application. In that form i used button to browse and select file, text box to display selected file path, and another button(Send) to transfer that file through WCF service. But i am unable to access textbox value in the transfer solution. it shows"the name does not exist in the current context".
Code for TransferService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TransferService" in both code and config file together.
public class TransferService : ITransferService
{
public File DownloadDocument()
{
File file = new File();
String path = txtSelectFilePath.Text;
file.Content = System.IO.File.ReadAllBytes(#path);
//file.Name = "ImagingDevices.exe";
return file;
}
}
}
I am getting error on this line
String path = txtSelectFilePath.Text;
code for form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sender
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Browse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtSelectFilePath.Text = openFileDialog1.FileName;
}
}
private void Send_Click(object sender, EventArgs e)
{
TransferService.TransferServiceClient client = new TransferService.TransferServiceClient();
TransferService.File file = client.DownloadDocument();
System.IO.File.WriteAllBytes(#"C:\DownloadedFiles\" + file.Name, file.Content);
MessageBox.Show(file.Name + " is downloaded");
}
}
}
Code for ITransferService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITransferService" in both code and config file together.
[ServiceContract]
public interface ITransferService
{
[OperationContract]
File DownloadDocument();
}
[DataContract]
public class File
{
[DataMember]
public string Name { get; set; }
[DataMember]
public byte[] Content { get; set; }
}
}
Thanx a lot in advance..........
Then create a constructor to your class that receives a path as string something like this:
public class TransferService : ITransferService
{
private string _path;
public TransferService(string path) {
_path = path
}
public File DownloadDocument()
{
File file = new File();
//String path = txtSelectFilePath.Text;
file.Content = System.IO.File.ReadAllBytes(_path);
//file.Name = "ImagingDevices.exe";
return file;
}
}
and then on form.cs
TransferService.TransferServiceClient client = new TransferService.TransferServiceClient(txtSelectFilePath.Text);
I am trying to read JSON data from web, and i am using Json.net library for this task, the problem is if there is only one json object
{"id":"2340","time":"2014-10-29 16:26:49"}
everything works fine, but if there is an array of them
[{"id":"2340","time":"2014-10-29 16:26:49"}, {"id":"2341","time":"2014-10-29 16:27:21"}]
Program isn't working.
using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Collections.Specialized;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace Localhostnet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString('http://localhost/json.php');
LocalhostTiming jsonResponse = JsonConvert.DeserializeObject<LocalhostTiming>(htmlCode);
string timeId = jsonResponse.id;
MessageBox.Show(timeId);
}
}
}
public class LocalhostTiming
{
public string id { get; set; }
public string time { get; set; }
}
}
I've tried to add
public class DataContainer
{
public List<LocalhostTiming> LocalhostTiming{ get; set; }
}
But i dont know how to work with this code.
You are trying to deserialize an array of LocalhostTiming into one instance of it.
You need to deserialize the object to an array.
List<LocalhostTiming> jsonResponse = JsonConvert.DeserializeObject<List<LocalhostTiming>>(htmlCode);