Oppure

Loading
29/08/17 13:56
Mikelius
Con conosco SQL , ma non credo si una cosa completamente diversa ragionare con esso:


txtName1.Text = reader["name"].ToString().Replace(" ", ""); 


Qui mi sa che inserisci dentro txtName1 , la stringa reader["name"] sostituendone gli spazii.
txtName1.Text = reader["name"].ToString(); 

Non sarebbe più giusto?

-------------------------------------------------


com.Parameters.AddWithValue("@name", txtName1.Text)


Il metodo, AddWithValue (String, Object), come secondo parametro accetto un Object. Nel tuo caso non sarebbe la stringa su txtName, quindi
com.Parameters.AddWithValue("@name", txtName1.Text.ToString() )

?




Ultima modifica effettuata da Mikelius 29/08/17 13:57
aaa
29/08/17 14:35
Quel codice che senso ha?

reader da dove viene ?

il replace a cosa serve? perché lo usi?

per scrivere c'è dell'altro codice o no? come esegui l'update?

Se il codice è solo quello, ovvio che non funziona ... non ha senso.

Altrimenti mostra TUTTO il codice
29/08/17 15:53
fosforo
ciao Mikelius

Nemmeno io conosco bene l'argomento SQL.
Ho fatto questa riga:
1.txtName1.Text = reader["name"].ToString().Replace(" ", "";);

Il motivo del Replace è che il campo "Nome" su SQL è di 50 caratteri.
Se non metto il Replace, la TextBox sarà del nome più i caratteri vuoti rimanenti per arrivare a 50.

Il mio problema è che a video vedo il campo "Nome" da SQL corretto, ma una volta cambiato manualmente
a tastiera, il valore txtName1.Text rimane quello letto dal DB.

Il valore effettivo della TextBox riscritta non viene preso in considerazione.
Di conseguenza nel DB viene riscritto lo stesso dato.


Cioè:
Leggo il DB e stampo nella TextBox il suo valore, mettiamo Carlo.
Scrivo nella TextBox il nuovo valore, mettiamo Luigi.
Nel DB viene riscritto Carlo.

Se guardo il valore di txtName1.Text durante il debug di VS, il valore di txtName1.Text è sempre Carlo.
Come se non si riaggiornasse con il nuovo valore.
aaa
30/08/17 6:49
Hai ripetuto il problema senza rispondere alle domande.

Perché risponderti se non dai i dettagli richiesti?
30/08/17 6:51
archemi
Come ti è stato giustamente fatto notare: dopo la modifica fai l'update del DB?
Cioè ExecuteNonQuery(); lo mandi??
aaa
30/08/17 8:12
fosforo
Spero non sia troppo lungo e se ci sono degli errori è perché sto cercando di capire dove sbaglio:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class UserPage : System.Web.UI.Page
{

string txtName2 = null;
protected void Page_Load(object sender, EventArgs e)
{
string str;
UTILITY.Visible = false;



if (Session["Session"] != null)
{
tblLogged.Visible = true; // Tabella tblLogged
str = Cache["B"].ToString(); // Name from cache
lblNameLogged.Text = str;

str = Cache["A"].ToString(); // Email from cache

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["stringa"].ConnectionString);
conn.Open();
string checkMail = "select count(*) from Users where email='" + str + "'";

SqlCommand com = new SqlCommand(checkMail, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

conn.Close();


if (temp == 1)
{
conn.Open();


str = "select * from Users where email='" + Cache["A"] + "'";
com = new SqlCommand(str, conn);

SqlDataReader reader = com.ExecuteReader();

if (reader.Read())
{
txtName.Text = reader["name"].ToString().Replace(" ", "";); // Replace toglie spazi alla fine
txtSurname.Text = reader["surname"].ToString().Replace(" ", "";);
txtEmail.Text = reader["email"].ToString().Replace(" ", "";);
txtMobile.Text = reader["mobile"].ToString().Replace(" ", "";);
txtState.Text = reader["state"].ToString().Replace(" ", "";);
txtCountry.Text = reader["country"].ToString().Replace(" ", "";);
txtCity.Text = reader["city"].ToString().Replace(" ", "";);
txtStreet.Text = reader["street"].ToString().Replace(" ", "";);
txtHousenumber.Text = reader["housenumber"].ToString().Replace(" ", "";);
txtZipcode.Text = reader["zipcode"].ToString().Replace(" ", "";);
txtJob.Text = reader["job"].ToString().Replace(" ", "";);
txtInterest.Text = reader["interest"].ToString().Replace(" ", "";);
txtHobby.Text = reader["hobby"].ToString().Replace(" ", "";);

string cc = reader["dob"].ToString(); // read dob from DB
DropYear.Items[0].Text = cc.Substring(cc.LastIndexOf("-";)+1,4);
DropMonth.Items[0].Text = cc.Substring(cc.IndexOf("-";) + 1, (cc.LastIndexOf("-";)-3));
DropDay.Items[0].Text = cc.Substring(0, cc.IndexOf("-";));
DropGender.Items[0].Text = reader["gender"].ToString().Replace(" ", "";);
}
conn.Close();
}
else
{
string script = "alert('LOGIN FAILED!');";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "LOGIN FAILED...", script, true);

tblLogged.Visible = false;
}
}

else
{
ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('NOT LOGGED IN...');location.href='/LoginPage.aspx'", true);
}
}

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{

try
{
//Response.Write(txtName1.Text);
Guid BeBoxID = Guid.NewGuid(); // create ID

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeBox"].ConnectionString);
conn.Open();
string insertQuery = "UPDATE Users SET name = @name";

SqlCommand com = new SqlCommand(insertQuery, conn);




com.Parameters.AddWithValue("@ID", BeBoxID.ToString());
com.Parameters.AddWithValue("@name", txtName.Text.ToString());
com.Parameters.AddWithValue("@surname", txtSurname.Text);
com.Parameters.AddWithValue("@email", txtEmail.Text);
com.Parameters.AddWithValue("@password", txtPass.Text);
com.Parameters.AddWithValue("@mobile", txtMobile.Text);
com.Parameters.AddWithValue("@city", txtCity.Text);
com.Parameters.AddWithValue("@street", txtStreet.Text);
com.Parameters.AddWithValue("@housenumber", txtHousenumber.Text);
com.Parameters.AddWithValue("@zipcode", txtZipcode.Text);
com.Parameters.AddWithValue("@country", txtCountry.Text);
com.Parameters.AddWithValue("@state", txtState.Text);
com.Parameters.AddWithValue("@job", txtJob.Text);
com.Parameters.AddWithValue("@interest", txtInterest.Text);
com.Parameters.AddWithValue("@hobby", txtName.Text);
com.Parameters.AddWithValue("@dob", dob.SelectedDate.ToString("MM/dd/yyyy";));



com.ExecuteNonQuery();

string script = "alert('Registration is successful...');";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Registration is successful...", script, true);

//Response.Redirect("/UserPage.aspx";);


conn.Close();
}
catch (Exception ex)
{
string script = "alert('ERROR!!') + ex;";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EDIT FAILED...", script, true);
Response.Redirect("/UserPage.aspx";);
}
}
protected void txtName_TextChanged(object sender, EventArgs e)
{
txtName.Text = txtName.Text.Replace(" ", txtName.Text);
}
}




Grazie anticipatamente
Ultima modifica effettuata da fosforo 30/08/17 15:14
aaa
30/08/17 12:41
Ma se nella Update c'è solo il nome, perché aggiungi tutti i valori con tutti quei parametri (inesistenti)?

E come mai nella Update manca la Where con la mail?
30/08/17 15:11
fosforo
Dato che non scrive nulla mi ero concentrato solo sul campo Nome.
Perché la mail non va riscritta.
aaa