DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   Update from Asp.net to Sql server db (http://www.daniweb.com/forums/thread198033.html)

nikichu Jun 17th, 2009 6:30 am
Update from Asp.net to Sql server db
 
...hope some body can help me resolve the problem....where i have to update a particular data ,
the datas are retreived from the db using a dropdownlist,selected data are updated inside a textbox...........
.....where there are constraints set by validators
for space and !^#$%&*^----charaecters:

My problm is data is not updated,please help me

Code:(Asp.net C#---Sql server db)


private void btedit_Click(object sender, System.EventArgs e)
                {
                        LabErr.Text="";
                        if(Page.IsValid==true)
                        {
                                SqlConnection con1=new SqlConnection(@"Server=sqloledb.1;User ID=sa;password=;Data Source=IRID_BDC1;Initial Catalog=newbie");
                                con1.Open();
                                SqlCommand cmd=new SqlCommand("Update Loc_mast Set Loc =('"+ txtrnme.Text+"') Where Loc =('"+ Droploc.SelectedItem.Text+"')",con1);
                                cmd.ExecuteNonQuery();
                        }
                        else
                        {
                        //txtrnme.Text="";
            LabErr.Text="Please try Again";
                        }


..please suggest a soln..........

ICHU

sknake Jun 17th, 2009 7:24 am
Re: Update from Asp.net to Sql server db
 
Change your query to be set to a string and break the debugger in there. Your drop down list might have an empty string value which would cause the update to not work. You are also not closing your SQL connection and you will run the connection pool out of available connections if your site has many users.

private void btedit_Click(object sender, System.EventArgs e)
{
  LabErr.Text=string.Empty;
  if(Page.IsValid)
  {
    using (SqlConnection con1=new SqlConnection(@"Server=sqloledb.1;User ID=sa;password=;Data Source=IRID_BDC1;Initial Catalog=newbie"))
    {
      con1.Open();
      string query = "Update Loc_mast Set Loc =('"+ txtrnme.Text+"') Where Loc =('"+ Droploc.SelectedItem.Text+"')";
      if (string.IsNullOrEmpty(Droploc.SelectedItem.Text))
        throw new Exception();
      int iTest = Convert.ToInt32(Droploc.SelectedItem.Text);
      using (SqlCommand cmd=new SqlCommand(query,con1))
      {
        cmd.ExecuteNonQuery();
      }
      con1.Close();
    }
  }
  else
  {
  //txtrnme.Text="";
    LabErr.Text="Please try Again";
  }
}

Try something like that and see if you get an exception. That should get you pointed in the right direction.

carobee Jun 18th, 2009 4:06 am
Re: Update from Asp.net to Sql server db
 
try debugging the code.perhaps the empty values are passed in the query

nikichu Jun 22nd, 2009 8:20 am
Re: Update from Asp.net to Sql server db
 
:'( ..Still not working

.....Im using 2 radio buttons one to add and nther to edit

if i switch from one button to another,the datas are not gettng inserted to sql db

......the same the case as edit....
errors still........

ICHU

carobee Jun 22nd, 2009 1:28 pm
Re: Update from Asp.net to Sql server db
 
add a watch in the values at this point
SqlCommand cmd=new SqlCommand("Update Loc_mast Set Loc =('"+ txtrnme.Text+"') Where Loc =('"+ Droploc.SelectedItem.Text+"')",con1);

mail2saion Jun 23rd, 2009 2:14 am
Re: Update from Asp.net to Sql server db
 
Could you please post the table structure of Loc_mast?

nikichu Jun 24th, 2009 12:29 am
Re: Update from Asp.net to Sql server db
 
Quote:

Originally Posted by mail2saion (Post 897468)
Could you please post the table structure of Loc_mast?

structure of Loc_mast(SQL SERVER)

Colname Datatype length
Slno int 4(generated automatically-(*identity)
Loc_no nvarchar 50



ICHU

mail2saion Jun 24th, 2009 12:36 am
Re: Update from Asp.net to Sql server db
 
Use Slno for update operation rather than loc_no. Where clause shoud be
Where Slno =('"+ Droploc.SelectedItem.Value+"')",con1)

When you bind text properties of dropdown list must bind also Value properties by Slno.

nikichu Jun 24th, 2009 2:54 am
Re: Update from Asp.net to Sql server db
 
Quote:

Originally Posted by mail2saion (Post 898456)
Use Slno for update operation rather than loc_no. Where clause shoud be
Where Slno =('"+ Droploc.SelectedItem.Value+"')",con1)

When you bind text properties of dropdown list must bind also Value properties by Slno.


"...updates only the first record in db"

:(

mail2saion Jun 24th, 2009 5:12 am
Re: Update from Asp.net to Sql server db
 
Please post your bind code & the qurey that you use to update.


All times are GMT -4. The time now is 6:52 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC