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

Recommended Answers

All 4 Replies

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

Thanks,

please tell me the exact code,,

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

use "+" instead of "&"
like

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.