Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public Add_Profile()
       {
           InitializeComponent();
           value = New_Add_Profile.id;
           edit_val = New_Add_Profile.edit;
           if(edit_val == 1)
           {
               SqlConnection conn = new SqlConnection(ConnectionString);
               conn.Open();
               SqlCommand cmd = new SqlCommand("select company_name,caption,address1,address2,mobileno,email,gst,cloudApi,username,password,selectusertype,activationkey from add_profile where company_id ='" + txtCompanyId.Text+"'", conn);
               DataTable dt = new DataTable();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               txtCompanyName.Text = ds.Tables[0].Rows[0][1].ToString();// Columns["company_name"].ToString();
               txtCaption.Text = ds.Tables[0].Rows[0][2].ToString();
               txtAddress1.Text = ds.Tables[0].Rows[0][3].ToString();
               txtAddress2.Text = ds.Tables[0].Rows[0][4].ToString();
               txtMobile.Text = ds.Tables[0].Rows[0][5].ToString();
               txtEmail.Text = ds.Tables[0].Rows[0][6].ToString();
               txtGstin.Text = ds.Tables[0].Rows[0][7].ToString();
               txtCloudApi.Text = ds.Tables[0].Rows[0][8].ToString();
               txtUsername.Text = ds.Tables[0].Rows[0][9].ToString();
               txtPassword.Password = ds.Tables[0].Rows[0][10].ToString();
               txtcombox.Text = ds.Tables[0].Rows[0][11].ToString();
               picUserimage.Source.ToString();// = dt.Columns["company_id"].ToString();
               txtActivationKey.Text = ds.Tables[0].Rows[0][13].ToString();
               cmd.ExecuteNonQuery();
               conn.Close();
           }
       }


What I have tried:

value = New_Add_Profile.id;
           edit_val = New_Add_Profile.edit;
           if(edit_val == 1)
           {
               SqlConnection conn = new SqlConnection(ConnectionString);
               conn.Open();
               SqlCommand cmd = new SqlCommand("select company_name,caption,address1,address2,mobileno,email,gst,cloudApi,username,password,selectusertype,activationkey from add_profile where company_id ='" + txtCompanyId.Text+"'", conn);
               DataTable dt = new DataTable();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               txtCompanyName.Text = ds.Tables[0].Rows[0][1].ToString();// Columns["company_name"].ToString();
               txtCaption.Text = ds.Tables[0].Rows[0][2].ToString();
               txtAddress1.Text = ds.Tables[0].Rows[0][3].ToString();
               txtAddress2.Text = ds.Tables[0].Rows[0][4].ToString();
               txtMobile.Text = ds.Tables[0].Rows[0][5].ToString();
               txtEmail.Text = ds.Tables[0].Rows[0][6].ToString();
               txtGstin.Text = ds.Tables[0].Rows[0][7].ToString();
               txtCloudApi.Text = ds.Tables[0].Rows[0][8].ToString();
               txtUsername.Text = ds.Tables[0].Rows[0][9].ToString();
               txtPassword.Password = ds.Tables[0].Rows[0][10].ToString();
               txtcombox.Text = ds.Tables[0].Rows[0][11].ToString();
               picUserimage.Source.ToString();// = dt.Columns["company_id"].ToString();
               txtActivationKey.Text = ds.Tables[0].Rows[0][13].ToString();
               cmd.ExecuteNonQuery();
               conn.Close();
Posted
Updated 11-Sep-22 19:53pm

1 solution

Either your dataset is returning no tables, or the table is returning no rows.

Start by eliminating the DataSet - use a DataTable instead when there is only one set of return data - and then use the debugger to identify the content of txtCompanyID.Text.
Then use SSMS to check that value against your DB - almost certainly it's not what you think it is, or your table doesn't contain what you think it does.

But ... 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?
 
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