Joaanaa.93 Posted May 22, 2013 at 02:33 PM Report #508351 Posted May 22, 2013 at 02:33 PM (edited) Olá Pessoal Eu tou com uma duvida em ir buscar um valor da base de dados e por para dentro da variavel este é o codigo : public partial class AddPaginas : System.Web.UI.Page { string nr ; protected void Page_Load(object sender, EventArgs e) { SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings["inovaConn"].ConnectionString; SqlCommand sqlCommand = new SqlCommand(); SqlDataReader sqldr; sqlCommand.Connection = sqlConn; sqlCommand.CommandText = "Select nr_revista From Revistas_Header where id = (Select MAX(id) From Revistas_Header)"; sqlConn.Open(); sqldr = sqlCommand.ExecuteReader(); lbl_nr.Text = sqldr.ToString(); } Comprimentos Edited May 22, 2013 at 02:45 PM by pmg GeSHi
nelsonr Posted May 22, 2013 at 02:40 PM Report #508352 Posted May 22, 2013 at 02:40 PM (edited) Experimenta com isto: sqldr = sqlCommand.ExecuteReader(); if(sqldr.Read()) lbl_nr.Text = sqldr.GetInt32(0).ToString(); (pode ser necessário mudar usar outro tipo, caso o campo não seja inteiro) Edited May 22, 2013 at 04:04 PM by nelsonr
bioshock Posted May 22, 2013 at 02:41 PM Report #508353 Posted May 22, 2013 at 02:41 PM (edited) O tópico devia estar em C#. string query = "Select nr_revista From Revistas_Header where id = (Select MAX(id) From Revistas_Header)"; SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings["inovaConn"].ConnectionString; SqlCommand sqlCommand = new SqlCommand(query, sqlConn); sqlConn.Open(); SqlDataReader dR = sqlCommand.ExecuteReader(); While(dR.Read()) { lbl_nr.Text = dR[0].ToString() } Edited May 22, 2013 at 02:42 PM by bioshock
pmg Posted May 22, 2013 at 02:45 PM Report #508356 Posted May 22, 2013 at 02:45 PM O tópico devia estar em C#. Tópico movido para quadro C#. What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!
Joaanaa.93 Posted May 22, 2013 at 03:58 PM Author Report #508363 Posted May 22, 2013 at 03:58 PM Tópico movido para quadro C#. Peço desculpa, como se tratava tambem de base de dados, pensava que podia ser na secção de base de dados. Experimenta com isto:Código (C#): sqldr = sqlCommand.ExecuteReader(); if(sqldr.Read()) lbl_nr.Text = sqldr.GetInt32(0); (pode ser necessário mudar usar outro tipo, caso o campo não seja inteiro) Da um género de erro : http://gyazo.com/28bd556d0e3cc4ab6eaec34ab5b0a5f7
nelsonr Posted May 22, 2013 at 04:03 PM Report #508366 Posted May 22, 2013 at 04:03 PM (edited) Da um género de erro : http://gyazo.com/28bd556d0e3cc4ab6eaec34ab5b0a5f7 Tens de ver o tipo do campo. No exemplo pus a ler como se fosse Int32, se for diferente tens de alterar Edited May 22, 2013 at 04:04 PM by nelsonr
pikax Posted May 22, 2013 at 04:04 PM Report #508367 Posted May 22, 2013 at 04:04 PM de que tipo e' o campo nr_revista? faz assim: lbl_nr.Text = sqldr.GetString(0); Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Joaanaa.93 Posted May 22, 2013 at 04:12 PM Author Report #508368 Posted May 22, 2013 at 04:12 PM O campo é int
bioshock Posted May 22, 2013 at 04:14 PM Report #508372 Posted May 22, 2013 at 04:14 PM Quando quiseres ver a resposta que dei, talvez chegues a algum resultado. Até lá..
Joaanaa.93 Posted May 22, 2013 at 04:15 PM Author Report #508373 Posted May 22, 2013 at 04:15 PM http://gyazo.com/025174fcac2a49e0c2f8849f15d90971
pikax Posted May 22, 2013 at 04:20 PM Report #508374 Posted May 22, 2013 at 04:20 PM experimenta assim: lbl_nr.Text = (sqldr[0] ?? "").ToString(); Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Joaanaa.93 Posted May 22, 2013 at 04:21 PM Author Report #508375 Posted May 22, 2013 at 04:21 PM Quando quiseres ver a resposta que dei, talvez chegues a algum resultado. Até lá.. Desculpa nao tinha visto, pensava que estava a citar o código para mudar de tópico. Está resolvido pela solução do bioshok Obrigada a todos 🙂
pikax Posted May 22, 2013 at 04:24 PM Report #508376 Posted May 22, 2013 at 04:24 PM (edited) Usa o ExecuteScalar string query = "Select nr_revista From Revistas_Header where id = (Select MAX(id) From Revistas_Header)"; SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings["inovaConn"].ConnectionString; SqlCommand sqlCommand = new SqlCommand(query, sqlConn); string nr; sqlConn.Open(); var obj = sqlCommand.ExecuteScalar(); nr = (obj ?? "").ToString(); EDIT: No caso de a query retornar 1 elemento(1 row e 1 coluna), deves usar o ExecuteScalar Edited May 22, 2013 at 04:25 PM by pikax Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
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