LINQ - forums - Asked By svm satya on 05-Jan-12 03:44 AM

Hi,


              my code is see below.

                    string cs = WebConfigurationManager.ConnectionStrings["ForumConnectionString"].ConnectionString;
        string insertThread = "INSERT Threads" + "(topicid,subject,replies," + "author,lastpostdate)"
            + "VALUES (@topicid,@subject,@replies," + "@author,@lastpostdate)";
        string getThreadID = "SELECT @@IDENTITY";
        string insertMessage = "INSERT Messages " + "(threadid,author,date,message)"
            + "VALUES (@threadid,@author,@date,@message)";


        SqlConnection con = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand(insertThread, con);


        
        //insert the Thread
        cmd.Parameters.AddWithValue("topicid", Request.QueryString["topic"]);
        cmd.Parameters.AddWithValue("subject", txtSubject.Text);
        cmd.Parameters.AddWithValue("replies", 0);
        cmd.Parameters.AddWithValue("author", txtEmail.Text);
        cmd.Parameters.AddWithValue("lastpostdate", DateTime.Now);
        con.Open();
        
        //get the Thread ID
        cmd.CommandText = getThreadID;
        string threadid = cmd.ExecuteScalar().ToString();
        cmd.ExecuteNonQuery();
        //insert the Message


        cmd.CommandText = insertMessage;
        //cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("threadid", threadid);
        cmd.Parameters.AddWithValue("author", txtEmail.Text);
        cmd.Parameters.AddWithValue("date", DateTime.Now);
        cmd.Parameters.AddWithValue("message", txtMessage.Text);
        cmd.ExecuteNonQuery();
        con.Close();
Response.Redirect("Messages.aspx?topic=" + Request.QueryString["topic"] + "&thread=" + threadid);


    }
  protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Threads.aspx?topic=" + Request.QueryString["topic"]);
    }  please convert this code into linq to sql.







Riley K replied to svm satya on 05-Jan-12 03:54 AM


You have to create the datacontext first

here is the eg to insert 

its very easy

// Create a new Order object.
Order ord = new Order
{
  OrderID = 12000,
  ShipCity = "Seattle",
  OrderDate = DateTime.Now
  // …
};
 
// Add the new object to the Orders collection.
db.Orders.InsertOnSubmit(ord);
 
// Submit the change to the database.
try
{
  db.SubmitChanges();
}

Regards
Sreekumar P replied to svm satya on 05-Jan-12 04:30 AM
Hi,

Create a DataContext by adding a Item (LinqtoSQL classes).
Drag and drop the Table to the File u will get the class.

DataClassesDataContext dataContext = new DataClassesDataContext("connectionString");
    Threads thread = new Threads
    {
      author = "author"
      ,
      lastpostdate = DateTime.Now
      ,
      replies = "replies"
      ,
      subject = "subject"
      ,
      topicid = Convert.ToInt32(Request.QueryString["topic"])
    };
    dataContext.Threads.InsertOnSubmit(thread);
    try
    {
      dataContext.SubmitChanges();
    }
    catch { }


forums - svm satya replied to Sreekumar P on 05-Jan-12 09:35 PM

thank  very much
forums - svm satya replied to Sreekumar P on 05-Jan-12 09:44 PM
 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewThread.aspx?topic=" + Request.QueryString["topic"]);
    }
    protected void GridView1_SelectIndexChanged(object sender, EventArgs e)
    {
        string threadid = GridView1.SelectedValue.ToString();
        string topicid = Request.QueryString["topic"];
        Response.Redirect("Messages.aspx?topic=" + topicid + "&thread=" + threadid);
    }
}

i want this code in linq to sql
Sreekumar P replied to svm satya on 05-Jan-12 11:43 PM
Hi,

:) , actually the LINQ to SQL is only meant for Database related Queries and CRUD operations.

U can't convert this code into LINQ... this code will look like this only ..