cant update into the all column [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The c# code:
protected void check_click(object sender, EventArgs e)
{
SqlConnection c;
string str = "Data Source =(LocalDB)\\MSSQLLocalDB;";
str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;";
str += "Integrated Security= True";
c = new SqlConnection(str);
if (Page.IsValid)
{
Session["tit"] = (string)title.Text;
Session["Discrip"] = (string)Discrip.Text;
Session["Doname"] = (string)Dinoname.Text;
Session["wage"] = (string)wage.Text;
Session["lo"] = (string)ddlcountry.SelectedItem.Text;
Session["val"] = (bool)true;
Response.Redirect("Had Manger.aspx", true);
SqlCommand zer = new SqlCommand("UPDATE [User] SET did4 = 0", c);
c.Open();
zer.ExecuteNonQuery();
c.Close();
}
}
there is not an error but it is not updating
plss help
ok i update ehis is all the function

You are calling Response.Redirect("Had Manger.aspx", true); before you even hit your query execute. Comment that line out or move it to the end and you should be ok.

Response.Redirect("Had Manger.aspx", true);

Related

C# | Login doesn't work [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I can't figure it out why my Login system doesn't work.
It always resulting "FALSE" or "WRONG PASSWORD" tho I've entered the right password.
And yes, I've checked the column name and the table name as well. Nothing wrong but doesn't work.
I can't figure it out.
namespace GitGut {
public partial class Login : Window {
public String thisName;
private String thisConnectionString = #"Data Source = THISISMINE\SQLEXPRESS;Initial Catalog = GitGut; Integrated Security = True";
public Login() {
InitializeComponent();
}
private void Button_Login_Click(object sender, RoutedEventArgs e) {
Console.WriteLine("Button_Login pressed.");
if(theValidation(TextBox_Username.Text, PasswordBox_Password.Password) == true) {
Dashboard Form_Dashboard = new Dashboard();
Form_Dashboard.Show();
this.Hide();
} else {
MessageBox.Show("ERROR", "ERROR");
}
}
private Boolean theValidation(String thisUsername, String thisPassword) {
SqlConnection thisSqlConnection = new SqlConnection(thisConnectionString);
SqlCommand thisSqlCommand = new SqlCommand();
thisSqlCommand.Parameters.Add("#Username", SqlDbType.VarChar).Value = thisUsername;
thisSqlCommand.Parameters.Add("#Password", SqlDbType.VarChar).Value = thisPassword;
String thisQuery = "SELECT * FROM [User] WHERE [Username] = '#Username' AND [Password] = '#Password'";
thisSqlCommand.Connection = thisSqlConnection;
thisSqlCommand.CommandText = thisQuery;
thisSqlConnection.Open();
SqlDataReader thisSqlDataReader = thisSqlCommand.ExecuteReader();
if(thisSqlDataReader.Read()) {
thisSqlConnection.Close();
return true;
} else {
thisSqlConnection.Close();
return false;
}
}
}
}
You should remove the ' from the query string:
String thisQuery = "SELECT * FROM [User] WHERE [Username] = #Username AND [Password] = #Password";
The Sqlcommand parameters does not need any separator/indicator. You are already telling what type they are in thisSqlCommand.Parameters.Add("#Password", SqlDbType.VarChar)..., so the query string is correctly constructed.

Strange C# error while inserting in database [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have 2 forms in my program. I am using a SQL Server Compact 3.5 database file (.sdf) from C#
albums_tbl table has two columns: id, name
In form 1 when I use this code :
private void button1_Click(object sender, EventArgs e)
{
SqlCeCommand cm = new SqlCeCommand("INSERT INTO albums_tbl(album_name) VALUES (#album_name) ", cn);
cm.Parameters.AddWithValue("#album_name", textBox1.Text);
int affectedrows = cm.ExecuteNonQuery();
if (affectedrows > 0)
{
MessageBox.Show("insert shod !");
}
}
the program inserts well, but when I use the exact code in form 2 it errors this when I want to insert :
You don't open connection yet.
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Refer: https://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection(v=vs.100).aspx
Hope this helps.
You have not opened a connection , you just simply need to do as follow
private void button1_Click(object sender, EventArgs e)
{
SqlCeConnection con=new SqlCnConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
SqlCeCommand cm = new SqlCeCommand("INSERT INTO albums_tbl(album_name) VALUES (#album_name) ", cn);
cm.Parameters.Add(new SqlCeParameter(#album_name, textBox1.Text));
con.Open();
cm.ExecuteNonQuery();
}

Login Form using sql database c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a sign up page . you put user name and password and its save in the sql database.
note: evrey thing worked until i add the 2nd column(the password).
this is the password code(the username is the same) :
static public void delete(string _Password)
{
try
{
connection.Open();
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table] VALUES(#Password)", connection);
commandInsert.Parameters.Add("Password", _Password);
commandInsert.ExecuteNonQuery();
}
catch (SqlCeException expection)
{
MessageBox.Show(expection.ToString());
}
finally
{
connection.Close();
}
}
And this is the button settings :
private void button1_Click(object sender, EventArgs e)
{
if (deleteBox.Text != "")
{
SQLFunctions.Insert(insertBox.Text);
SQLFunctions.delete(deleteBox.Text);
SQLFunctions.Refresh(this.dataGridView1);
}
else
{
MessageBox.Show("login failed");
}
}
thanks
I guess you need to modify your code little bit like this
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table] VALUES(#username,#Password)", connection);
you should pass two parameters as you have two fields in db now and if you don't want to pass username then you should specify this thing like this
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](password) VALUES(#Password)", connection);
and also
commandInsert.Parameters.Add("Password", _Password);
this should be written as follows
commandInsert.Parameters.Add("#Password", _Password);
and to pass values to this parameter, you can do it like this
command.Parameters.AddWithValue("#Password", insertBox.Text);

what is the query of sql database connection [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to connect my C# project with SQL database.
which query I have to follow??
using(SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
}
i use this, it is not working
Try this
namespace Town
{
class clsconnection
{
string connection = #"Data Source=server name;Initial Catalog=databasename;Integrated Security=True";
public SqlConnection getconnection()
{
SqlConnection sql = new SqlConnection(connection);
sql.Open();
return sql;
}
}
}

ExecuteReader: Connection property has not been initialized. Browser Game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We are having some problems displaying the experience a player has from our database.
This is for a school project, would like some hints :)
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid");
string uID = Session["userID"].ToString();
CheckExp.Parameters.AddWithValue("#uid", uID);
try
{
connection.Open();
SqlDataReader ExpReader = null;
ExpReader = CheckExp.ExecuteReader();
if (ExpReader.Read())
{
Label6.Text = ExpReader["Experience"].ToString();
}
}
catch (Exception ex)
{
Label6.Text=(ex.Message);
}
finally
{
connection.Close();
}
You didn't connect your SqlConnection and SqlCommand.
Just define your connection as a second parameter like;
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid", connection);
Or you can assing your SqlCommand.Connection property like;
CheckExp.Connection = connection;

Categories

Resources