Dustin Stilwell
asked on
ASP.net/C# help for a fledgling
I am trying to build a small web application in ASP.net that texts our employees when we have an urgent message that we need to distribute. My idea is that someone with the authority to send one of these messages will login, create a message, and send it. I have been able to send messages successfully if name a single mobile number to a variable, or even when I assign multiple numbers to an array. Of course I don't want to go to the code to add/remove numbers, so I have a database with the numbers in it.
This is the part that is frustrating me is that I don't know how to get my data from SQL into my application. The closest I have gotten appears to assign the current location to my array, I am not even sure how that is happening.
[EDIT: the data I am trying to get from SQL is a single column of integers representing phone numbers.]
Here is my code asking SQL for data:
Here is the code for the Contacts class:
I created two text boxes so that I can output on the site the data that I think should be cell numbers, but that is not what is returned. The first image is the TxtCells box which is the result of looping through the List list and placing the values (I was expecting cell numbers) in the text box. The second image is the TxtList box which I just tried to print the list to without looping (no specific expectation here, just looking for anything I could to figure out why it wasn't working).
I have tooled around, looking for anything that can help and I have implemented a few proposed solutions but I am still having trouble.
Helpful information:
Using Visual Studio 2019/Testing in Edge
I am new to VS, ASP.net, C# and coding in general, so I wouldn't be surprised if I am so far off the beaten path that Lewis and Clark couldn't even find me. Perhaps this project was a little ambitious for a novice, but since I can't code, ambition is all I got!
I appreciate any help on this, as well as any constructive criticism.
This is the part that is frustrating me is that I don't know how to get my data from SQL into my application. The closest I have gotten appears to assign the current location to my array, I am not even sure how that is happening.
[EDIT: the data I am trying to get from SQL is a single column of integers representing phone numbers.]
Here is my code asking SQL for data:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace ProjectASPudemy.Pages
{
public partial class Comms : Page
{
private const string cs = "Data Source=ElRando\\SQLE;Initial Catalog=Inventory;Integrated Security=True";
private const string sql = "SELECT CellPhone, LastName FROM Employees WHERE CellPhone IS NOT NULL AND CurrentEmployee = 1";
private const int V = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSMS_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
Contacts[] aContacts = null;
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (var reader = cmd.ExecuteReader())
{
var list = new List<Contacts>();
while (reader.Read())
NewMethod(reader, list);
aContacts = list.ToArray();
TxtList.Text = list.ToString();
}
}
string contacts = null;
foreach (var contact in aContacts)
{
string number = contact.ToString();
contacts = contacts + contact.ToString() + "\n";
}
TxtCells.Text = contacts;
}
private static void NewMethod(SqlDataReader reader, List<Contacts> list)
{
list.Add(new Contacts { CellNumber = (int)reader.GetInt64(V) });
}
}
}
Here is the code for the Contacts class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjectASPudemy.Pages
{
public class Contacts
{
public int CellNumber { get; set; }
public static implicit operator Int32(Contacts v)
{
throw new NotImplementedException();
}
}
}
I created two text boxes so that I can output on the site the data that I think should be cell numbers, but that is not what is returned. The first image is the TxtCells box which is the result of looping through the List list and placing the values (I was expecting cell numbers) in the text box. The second image is the TxtList box which I just tried to print the list to without looping (no specific expectation here, just looking for anything I could to figure out why it wasn't working).
I have tooled around, looking for anything that can help and I have implemented a few proposed solutions but I am still having trouble.
Helpful information:
Using Visual Studio 2019/Testing in Edge
I am new to VS, ASP.net, C# and coding in general, so I wouldn't be surprised if I am so far off the beaten path that Lewis and Clark couldn't even find me. Perhaps this project was a little ambitious for a novice, but since I can't code, ambition is all I got!
I appreciate any help on this, as well as any constructive criticism.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Sorry, aContacts in an array of Contacts, it should be an array of int as long as the numbers are numerical values
Sorry, aContacts in an array of Contacts, it should be an array of int as long as the numbers are numerical values
ASKER
Hello Eduard,
I am sorry, but I don't understand.
In the Contacts class I have defined a CellNumber property that is an int. So I thought (my way of thinking) the aContacts accepting an int called CellNumber would work?
Following your suggestion of making aContacts an array of int rather than Contacts, let me know if I misunderstood this:
I have tried to remove my custom class 'Contacts' (It only has the one property anyway) and replace it with the int class, but when adding items to the list I think I have to name a property of the class, and, using int, I am not sure what property I can use. Is there a property I can use?
I am super confused but I am learning a lot trying to figure this out. I hope you can help me out of this and maybe teach me something about what I am doing wrong.
Thank you again for all of your help
I will be going home in a few minutes and I will likely not be back online until tomorrow morning. Should be roughly 15+ hours.
I am sorry, but I don't understand.
In the Contacts class I have defined a CellNumber property that is an int. So I thought (my way of thinking) the aContacts accepting an int called CellNumber would work?
Following your suggestion of making aContacts an array of int rather than Contacts, let me know if I misunderstood this:
I have tried to remove my custom class 'Contacts' (It only has the one property anyway) and replace it with the int class, but when adding items to the list I think I have to name a property of the class, and, using int, I am not sure what property I can use. Is there a property I can use?
I am super confused but I am learning a lot trying to figure this out. I hope you can help me out of this and maybe teach me something about what I am doing wrong.
Thank you again for all of your help
I will be going home in a few minutes and I will likely not be back online until tomorrow morning. Should be roughly 15+ hours.
Hi,
Your Contacts class (btw, it should be renamed as Contact) can have as many properties as required, but the code provided to you will extract just the CellNumber property and build a string with all the CellNumbers from the list.
Also, I see no use for the operator: public static implicit operator Int32(Contacts v)
Your Contacts class (btw, it should be renamed as Contact) can have as many properties as required, but the code provided to you will extract just the CellNumber property and build a string with all the CellNumbers from the list.
Also, I see no use for the operator: public static implicit operator Int32(Contacts v)
ASKER
Eduard,
Finally got it! I was still having trouble, but, after your last comment I decided to go back to the beginning and re-implement everything you had told me to do - and it worked! I had left off a cast that you had instructed me to place, and that was causing my headache. Had I not done that this would have been solved with your first response.
I apologize for wasting some of your time, but I am very grateful you were able to help!
Finally got it! I was still having trouble, but, after your last comment I decided to go back to the beginning and re-implement everything you had told me to do - and it worked! I had left off a cast that you had instructed me to place, and that was causing my headache. Had I not done that this would have been solved with your first response.
I apologize for wasting some of your time, but I am very grateful you were able to help!
Hi!
I'm happy that it's working! If you need more help, just contact me.
I'm happy that it's working! If you need more help, just contact me.
ASKER
I have made the changes that you suggested and now on the following line I am receiving the following error:
Open in new window
Error CS0029 Cannot implicitly convert type 'int[]' to 'ProjectASPudemy.Pages.ConThe only reason for the textboxes is so that I could 'see' the contents of the list 'list', and the array 'aContacts'. Those textboxes will go away when I know the array is working the way I want it to. In the array I want to see the values from the SQL db field CellPhone (the data type of that field in the db is bigint). They are just phone numbers though.
BigPicture:
I want to pull cellphone numbers from the db, place them into an array to be later processed by the service that will send the messages.
I hope that helps clarify a bit better!
Thanks for the help.