I got a simple winform app, basically it just loads data from the DB into a gridview and displays it, in order to do it i'm using a DataAdapter, I have around 7000 rows to show, in my local computer (Win7) it works fine, in the server howerver (2008 server) it loads the data, shows at least the first screen (although it doesn't show the scrollbar on the right to scroll down) and then it goes to Not Responding.
Trying to find the problem, I made sure the .net framework 4.0 is installed
string cs = ConfigurationManager.ConnectionStrings[csName].ConnectionString;
try
{
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter adapter = new SqlDataAdapter("sp_getAllDocuments", con);
adapter.SelectCommand.CommandTimeout = 600;
dt = new DataTable();
adapter.Fill(dt);
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = ex.ToString();
}
I even tried printing any possible Exception to a label there, but since it goes to not responding nothing else happens.
When I cut down the results from 7000 to 4 then it works, any ideas?, the sever is a good computer.
Change the Timeout to 10 secs and try
adapter.SelectCommand.CommandTimeout = 10000;
May be the time out is the problem
Maybe it just takes longer in production environment... could that be it?
Have you considered using a DataReader instead of the DataAdapter? I think the DataReader will be better in your case
http://msdn.microsoft.com/en-us/library/ms254931.aspx
Related
I don't get it why my (very simple) code is working properly on my local machine from Visual Studio 2022 and on the local IIS 10 to connect to a sql server express (15) and on my webserver it's not. I'm sure that's a really simple quesion for you.
What I'm tryin' to do is a simple login page. My code in the Login.aspx is:
using System.Data;
using System.Data.SqlClient;
try
{
SqlConnection con = new SqlConnection(#"Data Source=BERLIN\SQLEXPRESS;Initial Catalog=membersarea; User ID=sa;Password=Test2022!");
SqlCommand sqlCmd = new SqlCommand("select * from useraccount where username=#userName and passWord=#Password", con);
sqlCmd.Parameters.AddWithValue("#userName", tbxUsername.Text.ToString());
sqlCmd.Parameters.AddWithValue("#passWord", tbxPassword.Text.ToString());
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd);
DataTable datatable = new DataTable();
sqlAdapter.Fill(datatable);
con.Open();
int i = sqlCmd.ExecuteNonQuery();
con.Close();
if (datatable.Rows.Count > 0)
{
Session["userName"] = tbxUsername.Text.ToString();
datatable.Dispose();
Response.Redirect("Content.aspx");
}
else
{
lblMessage.Text = "Benutzername oder Passwort falsch.";
lblMessage.ForeColor = System.Drawing.Color.DarkOrange;
lblMessage.Visible = true;
}
}
catch(Exception ex)
{
}
(I know that I'm not supposed to do this with the sa account, just to keep it simple... The only thing I do on the web server is to change the name of the sql server instance. Management Studio works fine with this User Id and Password on my web server. I installed the sql server using Plesk and I don't think it is working properly within plesk. Using the Management Studio I can restore backups, queries, create new accounts, etc.)
My Content.asps says Hi (including my name) and shows the Logout-Button. If you enter credentials that are not correct it says so and if you try to go to the content-Page without loggin' in you're redirected to the Login-page. That is what I want. Trouble is, it's not workin' on my webserver. It is simply doin' nothing. No error message, or something else. It takes a while, password is cleared again and username is still there. (Doesn't matter which credentials are used.)
I don't think that it comes to the first line of my code, and I don't know why. Are there DLLs that are needed, or what else did I forget? I'm pretty sure this is a absolute beginner problem but I can't figure it out.
Tried to fill in some code to alter the lblMessage, to find out where the problem starts, but nothing of it is displayed.
I think the trouble started when I checked the pattern web forms while creating the project. In the bin folder there is a "name-of-my-project".dll and a "name-of-my-project".pdb file. Those two are generated if you recreate your project. I'm pretty sure you guys know that - I did not. Or better I do know that they were generated, but not that they are necessary. (As I wrote before, I'm at the very beginning.)
In guess that in this *.pdb and/or *.dll the connection string is stored, too. When I recreate my project with the valid connection string for the web server and upload them, too - everything works as expected. Thank you, guys for your ideas.
I've been stumped on this problem for a while, and although I have searched up my problems online, I haven't had much luck. So in my Android program made in Xamarin, in the button click event, I try to send data to the server, however, I would get this error:
System.InvalidOperationException: Update requires the command clone to have a connection object. The Connection property of the command clone has not been initialized.
I am using this code at this point:
using (SqlConnection connection = new SqlConnection(cs))
{
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter($"Select * from Room", connection);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
Spinner roomsSPNR = FindViewById<Spinner>(Resource.Id.rooms);
int room = Convert.ToInt32(roomsSPNR.SelectedItem.ToString());
DataRow[] selected = rooms.Tables[0].Select($"RoomNo = {room}");
selected[0][3] = Id;
dataAdapter.Update(rooms); //Line giving the error
}
changeColors(Id);
I'm stumped on this, I can't really figure out why it is doing this. Can anyone help?
I have tried:
Setting the update.connection manually
Making an SQL command and putting the command builder's command into there(There is another error)
Setting the DataAdapter's update command to the method for the command builder
In the DataAdapter's initialization, I have changed the connection to the connection string
Many others that may not have any importance
Edit: Earlier in the code the program is able to read from the server and put it into 'rooms. On the other hand, I'm not familiar with making web services; is that what I need to do to allow it to update the database?
You cannot directly connect with a database in xamarin. You need to first create a webservice and use that web service to insert or fetch data from database.
If you need I will help you with webservices.
I searched and checked but did not see what I need. I am using a data adapter's UpdateCommand's ExecuteNonQuery to update a local Access database. When I run the code, the ExecuteNonQuery does not appear to finish before the next line of code is executed and therefore, the updated database is not reflected in my grid or my text boxes. If I put a messagbox right after the ExecuteNonQuery, then when I hit ok in the messagebox, the rest of the code is executed and the updated database is reflected in my grid and text boxes. Or if I step through the code in debug without the messagebox to hault execution, the update to the database is reflected on my form. But if I just run the code without something after the query to slow it down or stop it, the update is not shown in my controls.
OleDbConnection connection = new OleDbConnection(connString);
string sql = "Update Table1 set Country = '" + txtNewText.Text + "' where SomeNum =
1111";
connection.Open();
dAdapter.UpdateCommand = connection.CreateCommand();
dAdapter.UpdateCommand.CommandText = sql;
dAdapter.UpdateCommand.ExecuteNonQuery();
//MessageBox.Show("Pause");
dTable.Clear(); //Clear or it will add everything to what was there.
dAdapter.Fill(dTable);
row = dTable.Rows[0];
txtCompany.Text = row["Name"].ToString();
txtGenre.Text = row["Job"].ToString();
txtId.Text = row["Id"].ToString();
txtSomeNum.Text = row["SomeNum"].ToString();
txtCountry.Text = row["Country"].ToString();
I'm assuming that the update query is not finishing before I re-fill the data table. Can someone please help me with this. I looked for something to show the status of the update query but did not see what I need. What am I missing here???
Thank you!
VH
10/28/14 Last night I added dAdapter.UpdateCommand.ExecuteNonQuery() right after dAdapter.UpdateCommand.ExecuteNonQuery(); then everything worked. I was not sure if this was just something that took enough time to allow the query to finish and therefore causing the update to display, or if it was just some fluke "band-aide" that worked. I then replaced that with connection.Close(); and that worked as well. Again...I don't know if closing the connection caused the query to finish and the connection to close before moving to the next line of code, or if this was also just "fudging" it to work. I have seen other people having similar problems in my searching for answers but I have not seen any one explain what the problem is and why this happens or the most appropriate solution.
You should use the OleDbDataReader, since you aren't using SQL.
Provides a way of reading a forward-only stream of data rows from a data source.
An example:
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close();
}
This will iterate through each specified column allowing you to fill your data. Documentation can be found here.
A SQL select statement gets run when a user presses a button on my website, and I do this:
connection = new OleDbConnection();
connection.ConnectionString = [connection string];
connection.Open();
cmd = profile.Execute(mySQLStatement);
da = new OleDbDataAdapter(cmd);
table = new DataTable();
da.Fill(table);
90% of the time, this works just fine. But every once in a while, I get the OleDbException table or view does not exist on the line da.Fill(table). There doesn't seem to be a pattern of when this works and when it doesn't, though it's more likely to not work when the site isn't used for a minute or two... Like the session might be expiring. But the rest of the website (that does not require this database) works.
Any ideas of what might be happening or how to fix it?
I have not found a solution. As a temporary fix, whenever this happens, I just re-start the page.
Sometimes the content of mySqlStatement is not exactly correct, maybe?
Trap the exception dump mySqlStatement to a logfile (with date and time this happened), and then throw.
I have at small web-application that get some data from a SQLdb in Visual Studio 2010. When i try to display this, using a simple dropdownlist it takes around 15 sec the first time just after compilation and then after that 5 sec every request. I use a LINQ connection to db and a repeater to print everything out. It´s four columns and around 50 rows, so not so mutch data. It doesn´t matter if i try take 10 rows, same response time.
We have tested the same thing on another computer with VS2008 installed and the time was like 0.1 sec or something like that. I use .NET 3.5 and have installed all the latest updates (SP1 and so).
If i look at the trace i see it takes time at these rows in my code:
var cust = dbContext.Customers
.Select(c => new
{
c.customerID,
c.Email
}).Take(10);
repeater.DataSource = cust;
repeater.DataBind();
Anybody know what could be taking som mutch time?
Regards
Daniel
I have tried this other aproach:
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = CommandType.Text; //default
cmd.CommandText = "SELECT Email FROM Customer";
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
It takes just as long.. there must be some other more fundamental problem than the code to call the db i think?
Some more information:
I did som trace, this is first time page is loaded:
Begin Raise ChangedEvents 0,00219604937555932 0,000014
After repeater has printed 15,8138335259613 15,811637
End Raise ChangedEvents 15,8138801733289 0,000047
Second time:
Begin Raise ChangedEvents 0,00219604937555932 0,000014
After repeater has printed 5,25090396258066 5,248825
End Raise ChangedEvents 25095106283536 0,000047
What´s happening at ChangeEvents?
Get the actual SQL being generated by LINQ, copy-paste it to your database management tool and look at the execution plan to identify potential performance issues.
You can see the actual SQL by using Log property of the data context. In a console application, you can do this:
dbContext.Log = Console.Out;
// ...
Or, write the SQL to file like this:
using (var writer = new StreamWriter("sql.log")) {
dbContext.Log = writer;
// ....
}
Also, it might be worth adding ToList() or ToArray() at the end of your LINQ expression, just to make sure it is not unintentionally re-executed.
I have no idee what i did to make it work.. just kept on doing stuff at other places in the code and updated latest windows update.. hmm.. now it work like a charm, must be a bug in VS2010??
Thanks for all suggestions though, learned some great stuff!
Regards