Error 1 Operator '&' cannot be applied to operands of type 'string' and 'string'

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 19
Reputation: krany18 is an unknown quantity at this point 
Solved Threads: 0
krany18 krany18 is offline Offline
Newbie Poster

error

 
0
  #1
Nov 14th, 2008
i taken the three textboxes and one button to store the data in sqlserver .but the it giving the error.
the code is as follows...


  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12.  
  13. public partial class Default3 : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. SqlConnection con = new SqlConnection("user id=sa;password=vikram;database=kran");
  18. SqlCommand cmd = new SqlCommand("select * from dr", con);
  19. SqlDataReader dr;
  20. // cmd.CommandText = "select * from dr";
  21. cmd.Connection = con;
  22. con.Open();
  23. dr = cmd.ExecuteReader();
  24. cmd.CommandText="insert into dr values("&TextBox1.Text&", ' " &TextBox2.Text&" ', ' " &TextBox3.Text&" ')";
  25. TextBox1.Text=" ";
  26. TextBox2.Text= " ";
  27. TextBox3.Text=" ";
  28. dr.Close();
  29. con.Close();
  30.  
  31.  
  32.  
  33. }
  34. protected void Button1_Click(object sender, EventArgs e)
  35. {
  36.  
  37. }
  38. }

i written the code like that .please clarify my application..
if it is not correct please tel me the correct...
Last edited by peter_budo; Nov 14th, 2008 at 8:09 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 176
Reputation: Kusno is an unknown quantity at this point 
Solved Threads: 13
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster

Re: error

 
0
  #2
Nov 14th, 2008
Hi, You use the same SqlCommand variable to read and execute query.
Please create new SqlCommand variable to execute query...

Thanks,
NEVER NEVER NEVER GIVE UP
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: krany18 is an unknown quantity at this point 
Solved Threads: 0
krany18 krany18 is offline Offline
Newbie Poster

Re: error

 
0
  #3
Nov 14th, 2008
please tell me the exact code,,
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: krany18 is an unknown quantity at this point 
Solved Threads: 0
krany18 krany18 is offline Offline
Newbie Poster

Error 1 Operator '&' cannot be applied to operands of type 'string' and 'string'

 
0
  #4
Nov 14th, 2008
hai to all,,
once again i am sending the same code.but it giving same problem.please rectify my problem .it is urgent to me...
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection("user id=sa;password=vikram;database=kum");
        SqlCommand cmd = new SqlCommand(" ", con);
        con.Open();
        con.Close();
        



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con=new SqlConnection("user id=sa;password=vikram;database=kum");
        SqlCommand cmd=new SqlCommand();
        cmd.CommandText = "insert into kumar values( " & TextBox1.Text & " ,' " & TextBox2.Text & " ')";
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        TextBox1.Text = " ";
        TextBox2.Text=" ";


    }
}
Error comes at bold line code..
please forward me a correct code..
Last edited by peter_budo; Nov 14th, 2008 at 8:11 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 144
Reputation: sierrainfo is an unknown quantity at this point 
Solved Threads: 9
sierrainfo sierrainfo is offline Offline
Junior Poster

Re: Error 1 Operator '&' cannot be applied to operands of type 'string' and 'string'

 
0
  #5
Nov 14th, 2008
use "+" instead of "&"
like

cmd.CommandText = "insert into kumar values( " + TextBox1.Text + " ,' " + TextBox2.Text + " ')";
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC