0

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

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=vikram;database=kran");
        SqlCommand cmd = new SqlCommand("select * from dr", con);
        SqlDataReader dr;
    //    cmd.CommandText = "select * from dr";
        cmd.Connection = con;
        con.Open();
        dr = cmd.ExecuteReader();
      cmd.CommandText="insert into dr values("&TextBox1.Text&", ' " &TextBox2.Text&" ', ' " &TextBox3.Text&" ')";
        TextBox1.Text=" ";
        TextBox2.Text= " ";
        TextBox3.Text=" ";
         dr.Close();
        con.Close();
                  
       

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
}

i written the code like that .please clarify my application..
if it is not correct please tel me the correct...

3
Contributors
4
Replies
6
Views
8 Years
Discussion Span
Last Post by sierrainfo
0

Hi, You use the same SqlCommand variable to read and execute query.
Please create new SqlCommand variable to execute query...

Thanks,

0

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();
        [B]cmd.CommandText = "insert into kumar values( " & TextBox1.Text & " ,' " & TextBox2.Text & " ')";[/B]
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        TextBox1.Text = " ";
        TextBox2.Text=" ";


    }
}

Error comes at bold line code..
please forward me a correct code..

0

use "+" instead of "&"
like

cmd.CommandText = "insert into kumar values( " + TextBox1.Text + " ,' " + TextBox2.Text + " ')";

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.