Anorco Posted May 20, 2009 at 11:08 AM Report #265750 Posted May 20, 2009 at 11:08 AM Estou a fazer um programa em c# 2008 com ligação ao sql server 2005. Tenho o seguinte código para fazer aparecer o registo anterior: #region Building the connection string string Server = "eu\\locadora"; string Username = "sa"; string Password = "******"; string Database = "locadora"; string ConnectionString = "Data Source=" + Server + ";"; ConnectionString += "User ID=" + Username + ";"; ConnectionString += "Password=" + Password + ";"; ConnectionString += "Initial Catalog=" + Database; #endregion #region Try to establish a connection to the database SqlConnection SQLConnection = new SqlConnection(); try { SQLConnection.ConnectionString = ConnectionString; SQLConnection.Open(); // You can get the server version // SQLConnection.ServerVersion DataSet ds = new DataSet(); string SQLStatement = string.Format("SELECT *from clientes"); DataTable dt = new DataTable(); dt = ds.Tables[0]; int numrows = dt.Rows.Count; pos = pos - 1; if (pos < 0) pos = 0; //Código para ir buscar os resultados textnumerocliente.Text = dt.Rows[pos]["cod_clientes"].ToString(); textnomecliente.Text = dt.Rows[pos]["nome_cliente"].ToString(); textbicliente.Text = dt.Rows[pos]["bi"].ToString(); textmoradacliente.Text = dt.Rows[pos]["morada"].ToString(); textcodpostalcliente.Text = dt.Rows[pos]["cod_postal"].ToString(); textlocalidadecliente.Text = dt.Rows[pos]["localidade"].ToString(); textelefonecliente.Text = dt.Rows[pos]["telefone"].ToString(); textelemovelcliente.Text = dt.Rows[pos]["telemovel"].ToString(); textemailcliente.Text = dt.Rows[pos]["email"].ToString(); } catch (SqlException SqlEx) { MessageBox.Show(this, SqlEx.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception Ex) { // Try to close the connection if (SQLConnection != null) SQLConnection.Dispose(); // Create a (useful) error message string ErrorMessage = "A error occurred while trying to connect to the server."; ErrorMessage += Environment.NewLine; ErrorMessage += Environment.NewLine; ErrorMessage += Ex.Message; // Show error message (this = the parent Form object) MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error); } #endregion } Quando executo o programa ele dá o seguinte erro: "A error occurred while trying to connect to the server." cannot find table 0 Alguém me sabe dizer porque que isto esta a acontecer? Desde já agradeço
Betovsky Posted May 20, 2009 at 11:23 AM Report #265751 Posted May 20, 2009 at 11:23 AM Porque o teu DataSet irá estar vazio. E ao fazeres "dt = ds.Tables[0]; " vai dar erro porque não existem nenhuma table. "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk
Anorco Posted May 20, 2009 at 11:29 AM Author Report #265754 Posted May 20, 2009 at 11:29 AM Ok, mesmo retirando isso(que nao sei se é o certo) da o seguinte erro: there is no row at position 0
M6 Posted May 20, 2009 at 01:17 PM Report #265789 Posted May 20, 2009 at 01:17 PM Com o o próprio erro indica: não há qualquer registo na posição zero, ou seja, a query não retornou nada, ou melhor, retornou um conjunto vazio. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar."
bruno1234 Posted May 20, 2009 at 08:41 PM Report #265948 Posted May 20, 2009 at 08:41 PM Não estás a executar o comando sql. Nem a povoar o dataset ou a datatable. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now