I could not connect to database - asp.net

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.

Related

How to refresh a webpage once azure sql database table is updated

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?

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.

Fetching data from remote SQL server

I have developed a asp.net web application using visual studio 2010. I'm using SQL server 2008 as database. I need to fetch data from one of the remote server through LAN connection and have to store to my database through C# code. I am beginner to web development using asp.net and C#. please tell me a very simple way to solve this.
This is an example which I tried. Is this code OK or wrong please guide me with this?
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = #"Data Source=172.16.7.127\inhouse;User ID=****;Password= *****;";
con1.Open();
string str = "select * from t_his_events Where patientMRN="+hosp_no.Text;
SqlDataReader dr;
SqlCommand myCommand = new SqlCommand(str,con1);
dr = myCommand.ExecuteReader();
if (dr.HasRows)
{
Console.WriteLine("connection established");
}
}
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection(<connectionstring>);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM table";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
And now you have a DataSet containing the results of your query. As for determining your , I suggest Connection Strings as a great resource for picking the perfect format.
Where is your database name in connectionString you created on remote server
using System.Data.SqlClient;
using system.Data;
string cs = Data Source=172.16.7.127\inhouse;database=databasename;User ID=User id create on remote server;Password= Your password for the ;
SqlConnection conn = new SqlConnection(cs);
SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM tableName",conn);
DataSet ds = new DataSet();
adp.Fill(ds);
now you can use the dataset object that contain your table

Load report failed, If put SQL Command in place otherwise FormLoad

I have CrystalReport page, The below code is just load one time through FormLoad and by using !Page.IsPostBack and If I try to reload it again give me error "Load report failed."
if (!Page.IsPostBack)
{
cmd = new SqlCommand("SELECT * FROM [tbJournals]", cnn);
cnn.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable("tbl");
da.Fill(dt);
cnn.Close();
_rdStudents = new ReportDocument();
_rdStudents.Load(Server.MapPath("~\\Accounting\\Journal_Report.rpt"));
_rdStudents.SetDatabaseLogon("sa", "password", ".\\SQLEXPRESS", "GoldenDeveloper");
_rdStudents.SetDataSource(dt);
CrystalReportViewer1.ReportSource = _rdStudents;
CrystalReportViewer1.RefreshReport();
}
But What I want to do put this code under CommandButton to change in search variable to make SQL Statement for example like that
SELECT * FROM [tbJournals] where [Date] = '"+ txtDate.Text +"'
But once I put the code instead of FormLoad to CommandButton give me error "Load report failed."
Thanks In Advance ....
I think the next time, when you reload then the
(!IsPostBack)
{
//This works only when the first time page loads.
}
So the code written in the same will not get executed.Try to write the code in Page_Load and after this function
like:
protected void Page_Load(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT * FROM [tbJournals]", cnn);
cnn.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable("tbl");
da.Fill(dt);
cnn.Close();
_rdStudents = new ReportDocument();
_rdStudents.Load(Server.MapPath("~\\Accounting\\Journal_Report.rpt"));
_rdStudents.SetDatabaseLogon("sa", "password", ".\\SQLEXPRESS", "GoldenDeveloper");
_rdStudents.SetDataSource(dt);
CrystalReportViewer1.ReportSource = _rdStudents;
CrystalReportViewer1.RefreshReport();
}
Solved finally, The problem was in report path, I did "CrystalReportViewer" report path blank and putted the report path like that _rdStudents.Load(Server.MapPath("Journal_Report.rpt")); by using Server.MapPath

Database does not exist

Here is my code to back up a database.
These are my databases:
I got this as Error:
Database 'BakupDB' does not exist. Make sure that the name is entered
correctly. BACKUP DATABASE is terminating abnormally.
And the code for my web page:
public partial class Default2 : System.Web.UI.Page
{
string dbname = "BakupDB";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
//Mentioned Connection string make sure that user id and password sufficient previlages
sqlcon.ConnectionString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BakupDB.mdf;Integrated Security=True;User Instance=True";
//Enter destination directory where backup file stored
string destdir = "D:\\backupdb";
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("backup database "+dbname+" to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
Response.Write("Backup database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
}
}
What do I wrong?
It looks like you're trying to use a User Instance. As an aside (not 100% related to your question) I believe that this feature is deprecated in SQL Server Express 2012 (and have been on a deprecation path since SQL Server 2008).
Sorry for the aside. With regards to your question perhaps the following will help you:
Stack Overflow question
Accomplishing the task using SMO
I would say the Stack Overflow question is closer to the use case you're trying to achieve with executing the SQL command, but have referenced the SMO way in case you were interested or wanted to try a different approach. Based on the SO question it appears that your connection string:
sqlcon.ConnectionString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BakupDB.mdf;Integrated Security=True;User Instance=True";
Is missing a Database= section so perhaps it should read like:
sqlcon.ConnectionString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BakupDB.mdf;Integrated Security=True;User Instance=True;Database=BakupDB";
This code worked for me:
private void button5_Click(object sender, EventArgs e)
{
try
{
string dlink= #"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\jsdb.mdf; Database=jsdb; User Instance=True; Integrated Security=True;Asynchronous Processing= True";
SqlConnection dcc = new SqlConnection(dlink);
dcc.Open();
string database = "jsdb";
string blc = textBox1.Text; //(Its the location to save the file)
string nm = "dbBackup";
string dt =DateTime.Today.ToShortDateString();
string sl = "BACKUP DATABASE "+database+" TO DISK = '"+blc+"\\"+nm+"-"+dt+".bak'" ;
SqlCommand cql = new SqlCommand(sl,dcc);
cql.ExecuteNonQuery();
MessageBox.Show("Database backup completed successfully..");
dcc.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}

Resources