Click here to Skip to main content
15,513,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
gender ='"+a+"'
not update work pls answer

What I have tried:

string a;
private void button1_Click(object sender, EventArgs e)
{
if (radiomal.Checked)
{
a = radiomal.Text;
}
else
{
a = radiofem.Text;
}


SqlConnection abc = new SqlConnection("Data Source=DESKTOP-8BEJFRN\\SQLEXPRESS;Initial Catalog=assignment1;Integrated Security=True");
abc.Open();
SqlCommand bcd = new SqlCommand("update cust set [Nic No]='" + txtnicn1.Text + "',Name='" + txtnam1.Text + "',Address='" + txtadd.Text + "',[Mobile no]='" + txtcon1.Text + "',gender='"+a+"' where [Custermer Id] ='" + txtcusid1.Text + "'", abc);
bcd.ExecuteNonQuery();
MessageBox.Show("your update ");
error update gender pls help
Posted
Updated 18-Jul-18 5:29am
Comments
Manish K. Agarwal 18-Jul-18 2:00am    
are you getting the correct value in var a before you form your UPDATE SQL

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your app - and if you miss one, somebody else won't - and the problem you have noticed will vanish at the same time.
 
Share this answer
 
Comments
Member 13847218 18-Jul-18 2:01am    
only gender error
OriginalGriff 18-Jul-18 2:04am    
Trust me, what you are doing is wrong, and very dangerous. Fix it! Your problem will probably go at the same time.
Member 13847218 18-Jul-18 2:44am    
already male save database
update female
not change female
OriginalGriff 18-Jul-18 3:13am    
If you don't listen to people when they tell you what to do, is there any point at all in asking a question? I'm pretty sure there isn't much point in me answering ...
1. The best way to get help is to use best-practices. Besides the security implications, it makes it a lot easier to read in this instance.

2. You didn't post the actual form so we can't tell what you got going on for the radio boxes

3. Try debugging and see what value is assigned to a.

This is a rough draft of what your code should look like:
C#
private void button1_Click(object sender, EventArgs e) {

    int RowsAffected = -1;
    if (radiomal.Checked) { a = radiomal.Text; }
    else { a = radiofem.Text; }

    string ConnInfo = "Data Source=;Initial Catalog=;Integrated Security=True";
    string CmdText = "UPDATE cust   SET [Nic No] = @NicNo, Name = @Name, Address = @Address, [Mobile no] = @MobileNo, gender = @Gender  WHERE [Custermer Id] = @CustomerID";
    using (SqlConnection conn = new SqlConnection(ConnInfo)) {
        SqlCommand cmd = new SqlCommand(CmdText, conn);

        cmd.Parameters.AddWithValue("@NicNo", txtnicn1.Text);
        cmd.Parameters.AddWithValue("@Name", txtnam1.Text);
        cmd.Parameters.AddWithValue("@Address", txtadd.Text);
        cmd.Parameters.AddWithValue("@MobileNo", txtcon1.Text);
        cmd.Parameters.AddWithValue("@Gender", a);
        cmd.Parameters.AddWithValue("@CustomerID", txtcusid1.Text);

        conn.Open();
        RowsAffected = cmd.ExecuteNonQuery();
        conn.Close();
    }
    MessageBox.Show(string.Format ("Your update affected {0} rows", RowsAffected));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900