How to refresh a webpage once azure sql database table is updated - asp.net

I'm working on a webPage app using C# ASP.NET, it retrieves me data from Azure Sql Database once I click my submit button.
protected void BtnSubmit_Click(object sender, EventArgs e)
{
string connStr = "Server=*******";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string strQuery = "select **** from myTable";
SqlCommand sqlcmd = new SqlCommand(strQuery, conn);
Label1.Text = sqlcmd.ExecuteScalar().ToString();
conn.close();
}
Now I'm trying to get my wabPage label.text updated automatically with the latest uploaded records in Azure Sql Database table, without using clock control, how do I achieve that?

Related

Delete Asp.Net Databse Connection Tutorial. Receiving CS103 error code

I am new to programming and found these great tutorials on the Guru99.com site. Under the insert, update, Delete Asp.Net Database Connection Tutorial I was able to make the connection to the database successfully where the message came back "Connection Made".
The problem I am having is under the ASP.NET Read Database using the SQLDataReader. When I added the code and ran the program, I got a CS103 code which is The name 'sqlquery' does not exist in the current context. I thought that had to rem out the response.write and ccn.close statements thinking that the connection needed to be opened and still got the same error code. I hoping to find someone who are familiar with this tutorial. Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
String connetionString; //Variable declaration
SqlConnection cnn;
connetionString = #"Data Source=BMIS-JHanlon3\SQLEXPRESS;Initial Catalog=DemoDB;User ID=sa;Password=demo123"; //Set connection string
cnn = new SqlConnection(connetionString); //Assign connection
cnn.Open(); //open and close the connection
// Response.Write("Connection Made");
// cnn.Close();
SqlCommand command;
SqlDataReader dataReader;
String sql, Output = " ";
sql = "Select TutorialID,TutorialName from DemoDB";
command = new SqlCommand(sql, cnn);
dataReader = sqlquery.ExecuteReader();
while (dataReader.Read())
{
Output = Output + dataReader.GetValue(0) + " - " + dataReader.GetValue(1) + "</br>";
}
Response.Write(Output);
dataReader.Close();
command.Dispose();
cnn.Close();
}
}
}

Con.open error ASP.NET in Godaddy Plesk Hosting

I am struggling with my new website which I recently uploaded on Godaddy server.
Con.open() is the line which is causing error : The wait operation timed out
My Code: file.aspx.cs
SqlConnection con = new SqlConnection(#"Data Source=databaseserverurl;Network Library=DBMSSOCN;Initial Catalog=mydatabase;User ID=something;Password=something;");
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (con.State != ConnectionState.Open) {
con.Open();
}
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from mytable";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
For MySQL
You should use MySqlConnection, not SqlConnection.
For SQL Server
Most likely your databaseserverurl is wrong. Make sure your connection string corresponds to the one you have in the control panel. See https://de.godaddy.com/help/find-your-connection-strings-3323.
To ensure, open Sql Server Management Tool (or your favorite sql tool) and login your db with same server, user and password.

I could not connect to database

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=LENOVO;Initial Catalog=dbMACARON;Integrated Security=True"); SqlCommand com = new SqlCommand("Select * from PRODUCT");
public void Bind()
{
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
da.Fill(ds, "PRODUCT");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
com.Connection = con;
SqlDataReader dr;
dr = com.ExecuteReader();
string id = "0";
Source Error:
Line 31: protected void Page_Load(object sender, EventArgs e)
Line 32: {
Line 33: con.Open();
Line 34: com.Connection = con;
Line 35: SqlDataReader dr;
It said error on my con.Open could not open database, what did i go wrong?
sorry im fresh so i need guidance ,
there some source error it tells me
if you are using sql express edition try this:
SqlConnection con = new SqlConnection("Data Source=.\SQLExpress;Initial Catalog=dbMACARON;Integrated Security=True");
Try using port number with the DB server name
What is the authentication mode? Windows Authentication or User id/Password based?
If it is userId/password based, then you need to provide password also.
Try this as it is.
1. Go to Tools menu-> folder option-> view tab and unCheck the following checkbox with name that HIDE EXTENSION FOR KNWON FILE TYPES.
2.Now crate the file as sql with extension udl i.e sql.udl. It will ask you for confirmation press Yes.
Right click on that file and select open with OLEDB CORE DATASERVICE. It will open the dialog box as follows.
![enter image description here][2]
Go to provider tab and select Microsoft OLEDB PROVIDER FOR SQL SERVER.. AND THEN CLICK ON NEXT BUTTON.
Then copy and paste your server name from SQL SERVER MANAGEMENT STUDIO in the first textbox. on second number select which mode you are using and in the end select the database which you want to use from the dropdown.
Click on TEST CONNECTION button it will show you successfull connection .
finally close that window and now open that udl file with notepad. Copy the content starting from the provider name to the end and paste it into your coding page.
Thats it. It will not show you error again.
I HAVE FIX IT MY SELF!! BUT THANKS FOR EVERYONE FOR TRYING HELPING ME OUT !! THANKS YOU!!
what i do is i put double "\" between lenovo and ebg example in sqp connection..
EXAMPLE:
public partial class SignUp : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=LENOVO\EBG; Initial Catalog=dbMACARON; integrated security= True");
SqlCommand com = new SqlCommand("Select * from CUSTOMER");
public void Bind()
{
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
da.Fill(ds, "CUSTOMER");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
weird in this web it didt show double "\" in here... but nway thanks.

update query is not working in ASP.NET web application

using System.Data.SqlClient;
using System.Data.Sql;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(#"Data Source=GAGAN-PC\SQLEXPRESS;Initial Catalog=update_test;Integrated Security=True");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void delete_button_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("delete from update_delete where id like'"+TextBox1.Text+"'",con);
cmd.ExecuteNonQuery();
Response.Write("Control reached.");
con.Close();
Response.Write("Data successfully deleted.");
}
protected void update_button_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update update_delete set password ='"+TextBox3.Text+"' where id like'"+TextBox2+"'", con);
cmd.ExecuteNonQuery();
Response.Write("Control reached.");
con.Close();
Response.Write("Data successfully Updated.");
}
}
I am trying to implement update query but there is a little problem in it. I have used SQL Server as database and update_delete is a table in which there are 3 columns id,sname,password and I am trying to update password with respect to id.
Problem is when I click on update button control reaches cmd.ExecuteNonQuery(); no error is displayed. but updating is not taking place. what should I do. Please Please Please help me. Thanks in advance. :) :)
I'm just guessing here - if Id is a numeric datatype, then you cannot use LIKE with it.
Also: please use using()... blocks to ensure proper disposal and use parametrized queries to avoid SQL Injection attacks.
Write your UPDATE command like this:
protected void update_button_Click(object sender, EventArgs e)
{
// get the values to use
string idValue = Convert.ToInt32(TextBox3.Text.Trim());
string password = TextBox2.Text.Trim();
// define the query text with *parameters* !
string updateQuery = "update update_delete set password = #password where id = #ID";
// put things like SqlConnection and SqlCommand into "using()...." blocks
using (SqlCommand updCmd = new SqlCommand(updateQuery, con))
{
// define parameters and their values
updCmd.Parameters.Add("#password", SqlDbType.VarChar, 50).Value = password;
updCmd.Parameters.Add("#ID", SqlDbType.Int).Value = idValue;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Data successfully Updated.");
}
}
I suppose you get an Exception. I would kindly suggest to catch your exception and tell us the message... You can catch the exception using the debugger or a try-catch clause.
If you don't get an exception and "Control reached" message is displayed, you would have to use the formed SQL string to use it directly in SQL Server and see if there is a mistake in the SQL statement. I suppose that you somehow form an invalid SQL statement (eg using a non-existing ID).
Hope I helped!

link data from Textbox to SQL Database in ASP.net (C#)

I am attempting to create a web form where data from several text box's will enter data into a n SQL database I have created. My code is listed below, and the problem is that when it compiles, it acts as if it hasn't. The messagedisplay.text does not change, and the SQL database does not update. Does anyone know a solution?
protected void createButton_Click(object sender, EventArgs e)
{
string state = stateTextBox.Text;
string country = countryTextBox.Text;
string lake = lakeTextBox.Text;
SqlConnection connection = new SqlConnection("Data Source=.MetricSample;Initial Catalog=ElementID;"+ "Integrated Security=true;");
connection.Open();
try
{
using (SqlCommand command = new SqlCommand(
"INSERT INTO ResearcherID VALUES(#ResearcherFname, #ResearcherLName)", connection))
{
command.Parameters.Add(new SqlParameter("ResearcherFName", country));
command.Parameters.Add(new SqlParameter("ResearcherLName", state));
command.ExecuteNonQuery();
}
messageDisplay.Text = "DB Connection Successfull";
}
catch
{
messageDisplay.Text = "DB Connection Failed";
}
}
try this
using (SqlCommand sqlCmd = new SqlCommand("INSERT INTO ResearcherID (FieldNameForFirstName, FieldNameForLastName) VALUES (#ResearcherFname, #ResearcherLName)", sqlConn)) {
sqlCmd.Parameters.AddWithValue("#ResearcherFname", country);
sqlCmd.Parameters.AddWithValue("#ResearcherLName", state);
}
Also use connection.Open(); inside try

Resources