Jump to content

Recommended Posts

Posted

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

Posted

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

Posted

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."

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.