Update table row in database c# console app - c#

trying to update a column of null values with instagramIds, this is my current approach but the console app just keeps running and doesn't update any values in the database.
public static async Task<InstagramUser> ScrapeInstagram(string url)
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
// create html document
var htmlBody = await response.Content.ReadAsStringAsync();
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlBody);
// select script tags
var scripts = htmlDocument.DocumentNode.SelectNodes("/html/body/script");
// preprocess result
var uselessString = "window._sharedData = ";
var scriptInnerText = scripts[0].InnerText
.Substring(uselessString.Length)
.Replace(";", "");
// serialize objects and fetch the user data
dynamic jsonStuff = JObject.Parse(scriptInnerText);
dynamic userProfile = jsonStuff["entry_data"]["ProfilePage"][0]["graphql"]["user"];
List<String> columnData = new List<String>();
//Update database query
string connectionString = #"Server=myProject-dev-db.cothtpanmcn7.ap-southeast-2.rds.amazonaws.com;Database=Projectdb;User Id=testadmin;Password=U8gs7vb7C7yvakXf;MultipleActiveResultSets=true;Trusted_Connection=False;";
using (SqlConnection con = new SqlConnection(connectionString))
{
//get null values from database
string query = "Select * from ApplicationUser where InstagramId is null";
using (SqlCommand command = new SqlCommand(query, con))
{
command.Connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
columnData.Add(reader.GetString(0));
}
}
}
for (int index = 0; index < columnData.Count(); index++)
{
//get username and scrape info
var instagramInfo = new InstagramUser
{
Id = userProfile.id,
};
columnData.Add(instagramInfo.ToString());
}
SqlCommand cmd = new SqlCommand("Update ApplicationUser Set InstagramId = '" + columnData + "'" + "where InstagramUsername = '" + userprofile.username + "'", con);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
// create an InstagramUser
var instagramUser = new InstagramUser
{
FullName = userProfile.full_name,
FollowerCount = userProfile.edge_followed_by.count,
FollowingCount = userProfile.edge_follow.count,
Id = userProfile.id,
url = url
};
return instagramUser;
}
else
{
throw new Exception($"Something wrong happened {response.StatusCode} - {response.ReasonPhrase} - {response.RequestMessage}");
}
}
}
The current output:
{"FullName":null,"FollowerCount":0,"FollowingCount":0,"Id":"6978701146","url":null}
{"FullName":null,"FollowerCount":0,"FollowingCount":0,"Id":"6978701146","url":null}
{"FullName":null,"FollowerCount":0,"FollowingCount":0,"Id":"6978701146","url":null}
{"FullName":null,"FollowerCount":0,"FollowingCount":0,"Id":"6978701146","url":null}
{"FullName":null,"FollowerCount":0,"FollowingCount":0,"Id":"6978701146","url":null}
My current approach is to create a list, add all instagramIDs which are null to that list. From there I add all instagramIds to that list after scraping Instagram for their usernames e.g. https://www.instagram.com/therock/?__a=1
then I update the column InstagramUsername with their instagram Ids

Related

Showing list of bank in db on click of a button

I have 3 input (username,password and datasource) and a button (connect) by clicking on button list of bank in db will be demonstrate
\[HttpGet\]
public JsonResult search()
{
//try connect to make connection on sql engine
// if it could to connect get list of db and return
// that
var username = Request.QueryString["username"].ToString();
var password = Request.QueryString["password"].ToString();
var datasource = Request.QueryString["databaseConString"].ToString();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Server"] = datasource;
builder["Connect Timeout"] = 1000;
builder["Trusted_Connection"] = true;
builder["Integrated Security"] = false;
builder.Password = password;
builder.UserID = username;
List<string> list = new List<string>();
Console.WriteLine(builder.ConnectionString);
using (SqlConnection con = new SqlConnection(builder.ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(" **problem **SELECT name FROM sys.databases**"**,con))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
// # I have problem in this loop **
while (dr.Read())
{
//list.Add(dr.GetString());
Console.WriteLine("{0}",dr[0]);
}
//foreach(var item in list)
//{
// Console.WriteLine(list);
//}
}
return Json( JsonRequestBehavior.AllowGet);
}
}
}
I do not know How use sqlDataReader and add object to my list
**problem SELECT name FROM sys.databases
Here you should also need to know in which table under the database, you are going to search bank name you haven't specified that. For instance, you should write like beloww:
SELECT BankName FROM BankTable [dbo].[BankDatabase]
I do not know How use sqlDataReader and add object to my list
Well, you could fetch your database-entity from your table inside the while loop. Finally bind your property into your list. Your code should be as following:
[HttpGet]
public JsonResult search()
{
var username = Request.QueryString["username"].ToString();
var password = Request.QueryString["password"].ToString();
var datasource = Request.QueryString["databaseConString"].ToString();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Server"] = datasource;
builder["Connect Timeout"] = 1000;
builder["Trusted_Connection"] = true;
builder["Integrated Security"] = false;
builder.Password = password;
builder.UserID = username;
List<string> listData = new List<string>();
using (SqlConnection con = new SqlConnection(builder.ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT [BankName] FROM [DataBaseName].[dbo].[TableName]", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string bankName = reader.GetString(0);
listData.Add(bankNamee);
}
}
return Json(listData);
}
}
}
Note: Make sure reader.GetString(0) the order has followed accordingly. Means if the [BankName] is in first order theb it should be as reader.GetString(0). In addition, remember the data-type as well. For your information, here, I am getting each bankNamee and binding to the list I defined above listData.Add(bankNamee);
Output:

Getting a null when trying to return File( ) in my controller c# mvc

So i am getting a null when trying to return a file and don't know how to get the data from my method.
public ActionResult ExportFile(string[] response)
{
ClientStatement_Inventory theStatementPath = new ClientStatement_Inventory();
var thePath = theStatementPath.statementPath;
DBQueries getyears = new DBQueries();
getyears.GetSpecificYears(response);
var cd = new ContentDispositionHeaderValue("attachment")
{
FileNameStar = theStatementPath.statementPath
};
return File(theStatementPath.statementPath, theStatementPath.statementYear);
//return File(thePath, "appliction/pdf", Server.UrlEncode(response.ToString()));
Here is my method in a different class where i am trying to get the data:
public class DBQueries
{
// Access Connection String
//string connectionString = ConfigurationManager.ConnectionStrings["..."].ConnectionString;
public List<string> GetSpecificYears(string[] response)
{
// Acquiring SqlConnection as a resource
using (SqlConnection con = new SqlConnection("..."))
{
List<string> paths = new List<string>();
//Open Connection
con.Open();
if (response != null)
{
for (var i = 0; i < response.Length; i++)
{
if (response[i] == "2019")
{
using (SqlCommand cmd = new SqlCommand("select statementPath from ClientStatement_Inventory where statementYear = '" + response[i] + "';", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
paths.Add(reader.GetString(0));
}
}
}
}
}
}
return paths;
}
}
}
I am able to get the file path from this sql query but how can i pass that to my controller and download the file(s)?

how to keep appending the list of values of a column until we get a new value in another column?

I am trying to keep appending a list of values to lookaheadRunInfo.gerrits until I get a new lookaheadRunInfo.ECJobLink in the while loop,I tried to create a variable “ECJoblink_previous” to capture the previous ECJoblink and create a new list only when they are different and keep appending until ECJoblink_previous changes,I tried as below but its not working,what am I missing?
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = #"some query";
var ECJoblink_previous ="";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//Console.WriteLine(rdr[0] + " -- " + rdr[1]);
//Console.ReadLine();
lookaheadRunInfo.ECJobLink = rdr.GetString(0);
if (ECJoblink_previous == lookaheadRunInfo.ECJobLink)
{
//Keep appending the list of gerrits until we get a new lookaheadRunInfo.ECJobLink
var gerritList = new List<String>();
lookaheadRunInfo.gerrits = gerritList.Add(rdr.GetString(2));
}
ECJoblink_previous = lookaheadRunInfo.ECJobLink;
lookaheadRunInfo.UserSubmitted = rdr.GetString(2);
lookaheadRunInfo.SubmittedTime = rdr.GetString(3).ToString();
lookaheadRunInfo.RunStatus = "null";
lookaheadRunInfo.ElapsedTime = (DateTime.UtcNow - rdr.GetDateTime(3)).ToString();
lookaheadRunsInfo.Add(lookaheadRunInfo);
}
rdr.Close();
}
var lookaheadRunsInfo = new List<LookaheadRunInfo>();
LookAheadRunInfo lookaheadRunInfo;
var i = 0;
var ecJoblink_previous = string.Empty;
while (rdr.Read())
{
if (rdr.GetString(0) != ecJoblink_previous)
{
ecJoblink_previous = rdr.GetString(0);
if (i > 0)
{
lookaheadRunsInfo.Add(lookaheadRunInfo);
}
// Create a new lookaheadRunInfo
lookaheadRunInfo = new lookaheadRunInfo
{
ECJobLink = rdr.GetString(0),
UserSubmitted = rdr.GetString(2),
SubmittedTime = rdr.GetString(3).ToString(),
RunStatus = "null",
ElapsedTime = (DateTime.UtcNow - rdr.GetDateTime(3)).ToString(),
gerrits = new List<string>
{
rdr.GetString(2)
}
};
}
else
{
//Keep appending the list of gerrits until we get a new lookaheadRunInfo.ECJobLink
lookaheadRunInfo.gerrits.Add(rdr.GetString(2));
}
}

0x80131904 ASP.NET error: A network-related or instance-specific error occurred while establishing a connection to SQL Server

Okay, funky error codes.
So I got the above error after sending my website live on a server. Other websites are live on the same server using IIS. I'm using SQL Express.
Looking online says things such as "Use instance name" and "Check your connection string" all of which works. Furthermore, I have a back-end application attached to the same database which has no trouble getting data from it.
Any thoughts? If that isn't enough to go on feel free to ask for more details.
Relevant code below:
// Redirect to noKey page if we can't find a key
if (Request["key"] == null)
Response.Redirect("NoKey.html", true);
// Get some Course info
string first_name, last_name, email, telephone, course;
DateTime date;
using (var server = Connection.LTMConnection())
using (var command = server.CreateCommand())
{
command.CommandText = #"SELECT * FROM Keys INNER JOIN Courses ON Keys.course = Courses.id WHERE [key] = #key";
command.Parameters.AddWithValue("#key", Request["key"]);
using (var reader = command.ExecuteReader())
{
if (!reader.Read())
// No Key
Response.Redirect("NoKey.html", true);
first_name = reader["first_name"].ToString();
last_name = reader["last_name"].ToString();
email = reader["email"].ToString();
telephone = reader["telephone"].ToString();
course = reader["name"].ToString();
date = reader["date"] as DateTime? ?? DateTime.Now;
if (DateTime.Compare(date, DateTime.Today) < 0)
{
Response.Redirect("Expired.html", true);
}
}
}
PrefaceName.Text = first_name + " " + last_name;
PrefaceCourse.Text = course;
// Populate specialism fields
using (var server = Connection.LTMConnection())
using (var command = server.CreateCommand())
{
command.CommandText = #"SELECT * FROM FormSpecialism";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var panel = new Panel();
panel.CssClass = "table_3rd";
var checkbox = new CheckBox();
checkbox.Text = reader["text"].ToString();
SpecialismMapping.Add(panel, reader["text"].ToString());
panel.Controls.Add(checkbox);
DataSpecialismPanel.Controls.Add(panel);
}
}
}
using (var server = Connection.LTMConnection())
using (var command = server.CreateCommand())
{
command.CommandText = #"SELECT * FROM FormClientObj";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var panel = new Panel();
panel.CssClass = "table_3rd";
var checkbox = new CheckBox();
checkbox.Text = reader["text"].ToString();
ClientObjMapping.Add(panel, reader["text"].ToString());
panel.Controls.Add(checkbox);
DataClientObjPanel.Controls.Add(panel);
}
}
}
using (var server = Connection.LTMConnection())
using (var command = server.CreateCommand())
{
command.CommandText = #"SELECT * FROM FormPOSItems";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var panel = new Panel();
panel.CssClass = "table_3rd";
var checkbox = new CheckBox();
checkbox.Text = reader["text"].ToString();
POSMapping.Add(panel, reader["text"].ToString());
panel.Controls.Add(checkbox);
DataPOSItemsPanel.Controls.Add(panel);
}
}
}
}
catch (Exception ex)
{
DataQualifications.Text = ex.ToString();
//Response.Redirect("NoKey.html", true);
}
}
EDIT: Connection string for both back-end and website is as follows(with some details omitted)
public static readonly string LTM = #"
Data Source =portal.omit.com\SQLEXPRESS;
Initial Catalog = OmitTrainingManager;
User Id = LTM;
Password = OMIT;";

How do you use the MySql IN clause

I am trying to figure out how to use the MySql In cluse with ASP.NET C#. Here is my code
var WebSites = string.Join(",", wsl.Select(x => "'" + x.DomainUrl + "'").ToArray());
string q = "select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN (#Url)";
When I dont use parameters this code works. When I include parameters I get no results from the query.
Here is my code for parameters
cmd.Parameters.Add("#Urls", MySqlDbType.Text).Value = WebSites;
here is whole code
public static IList<Post> FindPostsByWebSiteList(IEnumerable<WebSite> wsl)
{
var pl = new List<Post>();
var WebSites = string.Join(",", wsl.Select(x => "'" + x.DomainUrl + "'").ToArray());
string q = "select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN (#Urls)";
using (MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["MySqlConnectionString"].ToString()))
{
using (MySqlCommand cmd = new MySqlCommand(q, con))
{
cmd.Parameters.Add("#Urls", MySqlDbType.Text).Value = WebSites;
con.Open();
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
var p = new Post();
p.Id = reader.GetInt32("Id");
p.Url = reader.GetString("Url");
p.Title = reader.GetString("Title");
p.Date = reader.GetDateTime("Date");
p.ImageUrl = reader.GetString("ImageUrl");
pl.Add(p);
}
return pl;
}
}
}
I have found the answer. Here it is
public static IList<Post> FindPostsByWebSiteList(string[] urls)
{
var pl = new List<Post>();
var urlArray = urls.Select((x,y) => "#url" + y.ToString()).ToArray();
var urlsJoined = string.Join(",", urlArray);
string q = string.Format("select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN ({0})", urlsJoined);
using (MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["MySqlConnectionString"].ToString()))
{
using (MySqlCommand cmd = new MySqlCommand(q, con))
{
for (int x = 0; x < urlArray.Length; x++)
{
cmd.Parameters.Add(urlArray[x], MySqlDbType.Text).Value = urls[x];
}
con.Open();
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
var p = new Post();
p.Id = reader.GetInt32("Id");
p.Url = reader.GetString("Url");
p.Title = reader.GetString("Title");
p.Date = reader.GetDateTime("Date");
p.ImageUrl = reader.GetString("ImageUrl");
pl.Add(p);
}
return pl;
}
}
}
You have referenced #Url instead of #Urls
maybe just a typo in your question though
The IN statement should expect an Array of strings, and you are passing a single string
Your final SQL is looking like this:
select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN ('url1,url2,url3')
Instead of
select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN ('url1', 'url2', 'url3')
Check this question:
Add List<int> to a mysql parameter

Categories