Skip to content

How to insert records into table in database by using asp.net with c#.net?

paparao

Asked Jul 27, 2007, 9:03 PM EDT1 answer
Share
LinkedInX
HI Frens, I have taken two text textboxs and one button as well as i have created one table with two fields.Now i want to insert records into backend table. Then i have written some code,but it didn't show any exception.however, data didn't insert into table. can anybody rectify my code? string connstring = "server=paparao;uid=sa;pwd=sa;database=training"; SqlConnection cnPrintegra = new SqlConnection(); DataSet dsPrintegra = new DataSet(); cnPrintegra.ConnectionString = connstring; cnPrintegra.Open(); SqlDataAdapter cmdUsers = new SqlDataAdapter("select * from example", cnPrintegra); SqlCommandBuilder cmdb = new SqlCommandBuilder(cmdUsers); cmdUsers.Fill(dsPrintegra, "example"); DataRow cRow ; cRow = dsPrintegra.Tables["example"].NewRow(); cRow[0] = TextBox1.Text; cRow[1] = TextBox2.Text; dsPrintegra.Tables["example"].Rows.Add(cRow); dsPrintegra.AcceptChanges(); cmdUsers.Update(dsPrintegra, "example"); cnPrintegra.Close();

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    hi !!!
    i hv the complete solution 2 ur problem..
    u can try this code..and i can assure u that ur problem is solved..
    here's the code..u can put it in ur button_click method

    protected void Button1_Click(object sender, EventArgs e)

    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["insertConnectionString"].ConnectionString);


    SqlCommand cmd=new SqlCommand("insert into Table1 values('"+ TextBox1.Text+"','"+TextBox2.Text+"')",conn);

    conn.Open();

    cmd.ExecuteNonQuery();

    TextBox1.Text = "";

    TextBox2.Text = "";

    Response.Write("submitted");

    Response.Redirect("Default2.aspx");

    }

    0