I am very new to Visual Studio. I am writing a program that will retrieve the signature of an employee from an API when their name is entered and a "Get user" button is clicked (e.g. if the employee name Jane Doe is entered, the signature "JNDO" will be retrieved from the API string when the name is entered and the button is clicked).
I have been able to read the data successfully with a text box; however, I would like to incorporate a combo-box, which suggests names as the user is typing (similar to Google).
When I try to run my code with a combo-box, the following is the error I get for my JSON:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered
while parsing value: <. Path '', line 0, position 0.' This exception
was originally thrown at this call stack:
[External Code]
TimeSheets_Try_11.Controllers.WebAPI.Getsignature(string) in WebAPI.cs
TimeSheets_Try_11.Form1.button1_Click(object, System.EventArgs) in Form1.cs
[External Code]
My JSON string:
[{"signature":"JNDO","firstName":"Jane","fullName":"Doe,
Jane","lastName":"Doe"}]
Code for my Windows form:
namespace TimeSheets_Try_11
{
public partial class Form1 : Form
{
WebAPI WA = new WebAPI();
public Form1()
{
InitializeComponent();
webBrowser1.Url = new Uri(StaticStrings.UrlIora);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DataSource = WA.Getsignature(textBox2.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 p = new Form2();
p.ShowDialog();
}
}
}
Code calling out my JSON:
namespace TimeSheets_Try_11.Controllers
{
class WebAPI
{
public string Getsignature(string name)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlIora), false);
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add("Cookie:" + cookies);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.UseDefaultCredentials = true;
string uri = "";
uri = StaticStrings.UrlIora + name;
var response = wc.DownloadString(uri);
var status = JsonConvert.DeserializeObject<List<Employeename>>(response);
string signame = status.Select(js => js.signature).First();
return signame;
}
Code Defining the variables in my JSON:
namespace TimeSheet_Try11_Models
{
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Employeename
{
public string signature { get; set; }
public string firstName { get; set; }
public string fullName { get; set; }
public string lastName { get; set; }
}
public class Root
{
public List<Employeename> Employeename { get; set; }
}
}
Related
The idea of this program is to retrieve the employee user ID (or signature) from an API URL once the name has been entered.
Right now my program shows no errors, however when I press start, my combo box (labeled "Name") doesn't show any suggestion as it is intended to. I would like it suggest name when the user is typing names (similar to how google works).
I have tried the ComboBox.DisplayMember, ComboBox.ValueMember, and ComboBox.DataSource, However, my data source is taken from an API JSON.
Thank you for your help in advance.
This is my JSON string:
[{
"signature": "JANDOW",
"firstName": "Jane",
"fullName": "Dow, Jane",
"lastName": "Dow"
}
]
My forms code is
namespace TimeSheets_Try_11
{
public partial class Form1 : Form
{
WebAPI WA = new WebAPI();
public Form1()
{
InitializeComponent();
webBrowser1.Url = new Uri(StaticStrings.UrlIora);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string sgname; string projectstring;
projectstring = comboBox1.Text.ToString();
sgname = WA.Getsignature(projectstring);
textBox2.Text = sgname;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(AutoCompleteStringCollection combData)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Code for calling out the JSON is:
namespace TimeSheets_Try_11.Controllers
{
class WebAPI
{
public string Getsignature(string name)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlIora), false);
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add("Cookie:" + cookies);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.UseDefaultCredentials = true;
string uri = "";
uri = StaticStrings.UrlIora + name;
var response = wc.DownloadString(uri);
var status = JsonConvert.DeserializeObject<List<Employeename>>(response);
string signame = status.Select(js => js.signature).First();
return signame;
}
}
}
Code for defining variables is:
namespace TimeSheet_Try11_Models
{
public class Employeename
{
public string signature { get; set; }
public string firstName { get; set; }
public string fullName { get; set; }
public string lastName { get; set; }
}
public class Root
{
public List<Employeename> Employeename { get; set; }
}
}
Firstly, the JSON is an array. You need to change your method to accommodate for that:
public List<string> Getsignature(string name)
{
...
var status = JsonConvert.DeserializeObject<List<Employeename>>(response);
return status.Select(emp => emp.signature).ToList();
}
Then you need to bind up your ComboBox correctly:
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DataSource = WA.Getsignature(textBox2.Text);
}
I would like to start by saying that I am not a developer and this is my very first time writing a code to this extend of complication (at least to me). Any help/guidance would be much appreciated.
The idea of this program is to retrieve the employee user ID (or signature) from an API URL once the name has been entered.
Right now my program shows no errors, however when I press start, my combo box (labeled "Name") doesn't show any suggestion as it is intended and no information is shown in textBox 2 (labeled "user ID").
I would like to add that initially I had a text box instead of a combo box, and I was able to retrieve data correctly then. The issue occurred when I switched to the combo box. Thank you in advance.
This is my JSON string:
[{"signature":"JANDOW","firstName":"Jane","fullName":"Dow, Jane","lastName":"Dow"}]
My forms code is:
namespace TimeSheets_Try_11
{
public partial class Form1 : Form
{
WebAPI WA = new WebAPI();
public Form1()
{
InitializeComponent();
webBrowser1.Url = new Uri(StaticStrings.UrlIora);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string sgname; string projectstring;
projectstring = comboBox1.Text.ToString();
sgname = WA.Getsignature(projectstring);
textBox2.Text = sgname;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
The code for calling out the API is:
namespace TimeSheets_Try_11.Controllers
{
class WebAPI
{
public string Getsignature(string name)
{
var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlIora), false);
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add("Cookie:" + cookies);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.UseDefaultCredentials = true;
string uri = "";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
uri = StaticStrings.UrlIora + name + "&%24format=json&%24top=30&%24filter=status%20eq%20%27Active%27&%24count=true";
var response = wc.DownloadString(uri);
var status = JsonConvert.DeserializeObject<List<Employeename>>(response);
string signame = status.Select(js => js.signature).FirstOrDefault();
return signame;
}
}
}
My code for defining the variables is:
namespace TimeSheet_Try11_Models
{
public class Employeename
{
public string signature { get; set; }
public string firstName { get; set; }
public string fullName { get; set; }
public string lastName { get; set; }
}
public class Root
{
public List<Employeename> Employeename { get; set; }
}
}
I have Form1 withDataGridView` which consists of the following columns:
ID
NAME
SHORT DESCRIPTION
DESCRIPTION
Also I have a button that opens a separate Form2 that has several textboxes.
How can I pass data from the second form to the original form, the value from textBox1 of Form2 will be passed to the ID column of the DataGridView?
It should be works same for all columns (textbox2 to NAME..textbox4 to DESCRIPTION)
public class MyClassName
{
public string ID { get; set; }
public string Name { get; set; }
public string DESCRIPTION { get; set; }
public string ShortDESCRIPTION { get; set; }
}
}
// Form2
public partial class Form2 : Form
{
public MyClassName mc;
public Form2()
{
InitializeComponent();
} // save text of your textboxs to your property public void.
public void SaveTextBoxesToMyObjectOfClass()
{
mc = new MyClassName();
mc.ID = textBox1.Text;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SaveTextBoxesToMyObjectOfClass();
Close();
}
}
}
\Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load+=new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MyClassName mc = new MyClassName();
Form2 f = new Form2();
f.ShowDialog();
this.dataGridView1.DataSource = mc;
}
}
If you mean like this it doesn't pass the data.
Could you please correct me?
You've got the code of Form1 in the class. Move that to a method or event handler, as Form_Load or to the form constructor.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load+=new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f= new Form2();
f.ShowDialog();
MyClass mc = f.mc;
}
}
Edit
This is how your Form2 should look like. Note mc is defined at class level,so it is accessible from outside the form:
MyClassName mc;
public Form2()
{
InitializeComponent();
} // save text of your textboxs to your property public void.
SaveTextBoxesToMyObjectOfClass()
{
mc = new MyClassName();
mc.ID = textBox1.Text;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SaveTextBoxesToMyObjectOfClass();
Close();
}
This would save your object on button click and close the form in that moment
I have a property Grid as follows:
I want to copy the complete content of the property grid to a data grid view(dataGeriView1) when submit button is clicked.
How to do this?
Please help.
private void Submit_Click(object sender, EventArgs e)
{
//propertyGrid1.SelectedObject = this;
dataGridView1.Columns.Add("Property", "Property");
dataGridView1.Columns.Add("Value", "Value");
GridItem gi = propertyGrid1.SelectedGridItem;
while (gi.Parent != null)
gi = gi.Parent;
foreach (GridItem item in gi.GridItems)
ParseGridItems(item); //recursive
dataGridView1.Sort(dataGridView1.Columns["Property"], ListSortDirection.Ascending);
}
private void ParseGridItems(GridItem gi)
{
if (gi.GridItemType == GridItemType.Category)
foreach (GridItem item in gi.GridItems)
ParseGridItems(item);
dataGridView1.Rows.Add(gi.Label, gi.Value);
}
Adapted from https://stackoverflow.com/a/12109186/1163434
Below is a sample snippet i have created to solve the above issue. Create a DataGridview by adding Columns Name,Age,Email,Phone.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Student std = new Student {Name = "Vimal" , Phone = "PhoneValue", Email="mymail",Age=24};
propertyGrid1.SelectedObject= std;
}
private void button1_Click(object sender, EventArgs e)
{
int index = dataGridView1.Rows.Count - 1;
Student std = (Student)propertyGrid1.SelectedObject;
dataGridView1.Rows[index].Cells["Name"].Value = std.Name;
dataGridView1.Rows[index].Cells["Age"].Value = std.Age;
dataGridView1.Rows[index].Cells["Email"].Value = std.Email;
dataGridView1.Rows[index].Cells["Phone"].Value = std.Phone;
}
}
public class Student
{
public int Age { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
This is my class, I always get a null insted of my panel...
Can someone give me a hint on how to do this?
[Serializable]
public class DragDropBlock : Panel
{
public DragDropBlock()
{
this.MouseDown += new MouseEventHandler(Mouse_Down);
this.MouseUp += new MouseEventHandler(Mouse_Up);
}
void Mouse_Down(object sender, System.Windows.Forms.MouseEventArgs e)
{
Clipboard.SetData("DragDropBlock", this);
}
void Mouse_Up(object sender, System.Windows.Forms.MouseEventArgs e)
{
IDataObject IBlock = Clipboard.GetDataObject();
DragDropBlock Block = (DragDropBlock)IBlock.GetData(typeof(DragDropBlock));
}
}
Given a class:
[Serializable]
class Test
{
public string Data
{
get;
set;
}
}
This works:
Test t = new Test()
{
Data = "DERP!"
};
Clipboard.SetData("Test", t);
Test newT = (Test)Clipboard.GetData("Test");
Console.WriteLine(newT.Data);
And if you want to use data objects:
Test t = new Test()
{
Data = "DERP!"
};
Clipboard.SetDataObject(new DataObject("Test", t));
Test newT = (Test)Clipboard.GetDataObject().GetData("Test");
Console.WriteLine(newT.Data);
The output to both of those is:
DERP!
This is the correction of my class: Working!!!
[Serializable]
class DragBlock
{
public string Data
{
get;
set;
}
}
public class DragDropBlock : Panel
{
DragBlock Block;
public DragDropBlock()
{
this.MouseDown += new MouseEventHandler(Mouse_Down);
this.MouseUp += new MouseEventHandler(Mouse_Up);
Block = new DragBlock()
{
Data = "TEST!"
};
}
void Mouse_Down(object sender, System.Windows.Forms.MouseEventArgs e)
{
Clipboard.SetDataObject(new DataObject("DragBlock", Block));
}
void Mouse_Up(object sender, System.Windows.Forms.MouseEventArgs e)
{
DragBlock newBlock = (DragBlock)Clipboard.GetDataObject().GetData("DragBlock");
Console.WriteLine(newBlock.Data);
}
}