C# .NET - dabata base replace - Asked By mohamed on 03-May-12 05:23 AM

hellow i have a page with textboxes >> on load page i fill all the textboxes from data base >>> then i have a update button i need a code when i click on button replace all the fields in the data base from text boxes
i used this code

using



System;

using



System.Collections.Generic;

using



System.Linq;

using



System.Web;

using



System.Web.UI;

using



System.Web.UI.WebControls;

using



System.Data.OleDb;

using



System.Data;

public



partial class admin_edit : System.Web.UI.Page

{


protected void Page_Load(object sender, EventArgs e)

{


string result = Request.QueryString["teer"];


string strSql, dbFile;


int teer;

teer =


int.Parse(result);

dbFile =


"Dbase.mdb";

strSql =


"SELECT * FROM [products] WHERE ([id] = " + teer + ")";


OleDbConnection c = new OleDbConnection();

c.ConnectionString =


"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + HttpContext.Current.Server.MapPath("~/admin/App_Data/" + dbFile);

c.Open();


OleDbCommand comm = new OleDbCommand();

comm.CommandText = strSql;

comm.Connection = c;


DataTable dt = new DataTable();


OleDbDataAdapter da = new OleDbDataAdapter(comm);

da.Fill(dt);

c.Close();

TextBox1.Text = dt.Rows[0][


"title"].ToString();

Image1.ImageUrl = dt.Rows[0][


"picture"].ToString();

DropDownList1.SelectedValue = dt.Rows[0][


"cat"].ToString();

TextBox5.Text = dt.Rows[0][


"price"].ToString();

TextBox2.Text = dt.Rows[0][


"info"].ToString();

TextBox4.Text = dt.Rows[0][


"picture"].ToString();

}


protected void Button1_Click(object sender, EventArgs e)

 {     here i want a code when i edit the text in textbox also replace the text in the field in database where id= "Request.QueryString["teer"];"

    

}

}

hope i get a full answer and thnks for all

kalpana aparnathi replied to mohamed on 03-May-12 05:37 AM
hi,

For solution try below example code:


using(SqlConnection con = new SqlConnection("connectionString"))
{
  string query = "INSERT INTO MyTable VALUES (@name, @lastName)";
  SqlCommand cmd = new Sqlcommand(query, con);
  cmd.Parameters.Add("@name", SqlDbType.Varcharm, 50).Value = textBoxName.Text;
  cmd.Parameters.Add("@lastName", SqlDbType.Varcharm, 50).Value = textBoxLastName.Text;
  cmd.Connection.Open();
  try
  {
    cmd.ExecuteNonQuery();  
  }
  catch{}  
}

Regards,
bharti odedra replied to mohamed on 03-May-12 05:41 AM
try this code
<asp:HyperLink ID="hlID" runat="server" Text='<%# EVAL("ID") %>' NavigateUrl='<%#"Default... + Replace(EVAL("ID"),"X", "") %>'/> Technorati Tags:  
Chintan Vaghela replied to mohamed on 03-May-12 05:44 AM

Hello,

Made your update logic as following way

 

protected void Button1_Click(object sender, EventArgs e)
 
  {
 
    string strSql, dbFile;
 
    dbFile = "Dbase.mdb";
 
    OleDbConnection c = new OleDbConnection();
 
    c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + HttpContext.Current.Server.MapPath("~/admin/App_Data/" + dbFile);
 
    OleDbCommand comm = new OleDbCommand();
 
    comm.Connection = c;
 
    strSql = "UPDATE [products] SET  [title] = @title, [picture]=@picture,[cat]=@cat,[price]=@price,[info]=@info,[picture]=@picture where [id] =" + Request.QueryString["teer"];
 
    comm.CommandText = strSql;
 
    //add our parameters to our command object
 
    comm.Parameters.AddWithValue("@title", TextBox1.Text);
 
    comm.Parameters.AddWithValue("@picture", Image1.ImageUrl);
 
    comm.Parameters.AddWithValue("@cat", DropDownList1.SelectedValue);
 
    comm.Parameters.AddWithValue("@price", TextBox5.Text);
 
    comm.Parameters.AddWithValue("@info", TextBox2.Text);
 
    comm.Parameters.AddWithValue("@picture", TextBox4.Text);
 
    c.Open();
 
    comm.ExecuteNonQuery();
 
    c.Close();
 
  }

 

 

 

Hope this helpful!

Thanks

 

 

 

Venkat K replied to mohamed on 03-May-12 05:46 AM
Simple Insert of data using LINQ.

NorthwindDataContext db = new NorthwindDataContext();
var newCustomer = new Customer
{
    CustomerID = TextBox1.Text,
    CompanyName = TextBox2.Text,
};
db.Customers.InsertOnSubmit(newCustomer);
db.SubmitChanges();

PS: If you are using Linqdatasource, just like sqldatasource's usages. 

dipa ahuja replied to mohamed on 03-May-12 05:55 AM


UPDATE Query
 
private void button1_Click(object sender, EventArgs e)
{
     SqlConnection connect = new SqlConnection(ConnString);
     connect.Open();
 
     string q = "Update people set name=@name where teer='"+ Request.QueryString["teer"] +"'"
 
     SqlCommand comm = new SqlCommand(q, connect);
     comm.Parameters.AddWithValue("name", txtname.Text);
     comm.ExecuteNonQuery();
     connect.Close();
 
}
 his will help you!!