i made a windows form application,i want to insert values in to two tables,do i have to use procedure
i wrote a procedure but i dont know how to insert the values (dont know the C# codes to inset values) ,

this is the procedure

Create Procedure AddMark
@RegNo int
,@AsNo int
,@Topic varchar(100)
,@Marks float 


INSERT INTO assignmentMarks 
values (@RegNo,@AsNO,@Marks)

insert into assignments


C#code which i wrote

int regno = Convert.ToInt16(textBoxRegNOO.Text);
         int assignment =Convert.ToInt16(textBoxAssignmentNo.Text);
         string ass_topic = textBoxTopic.Text;
         float mark = Convert.ToInt16(textBoxMarks.Text);

    var ab = db.Adds(regno,assignment,ass_topic,mark);

plz Help me , i m a beginner

Edited by Nick Evan: Added code-tags

7 Years
Discussion Span
Last Post by pauldani

if you use LinqToSql, there is an easier way to do the insert.
Check the following link for a good tutorial:


I have seen you recommend LINQ to SQL a number of times but not every query-related question is best solved with LINQ. Also I don't know that I would spend too much time and effort developing data access with LINQ since Microsoft has apparently killed the product:
Also: Microsoft kills linq to SQL

Now regarding the original post -- a stored procedure isn't required, this is a design decision you need to make about your database. You can achieve the insert with standard TSQL. The code for reading your user input doesn't make sense as far as data types go:

int regno = Convert.ToInt16(textBoxRegNOO.Text);
      int assignment = Convert.ToInt16(textBoxAssignmentNo.Text);

In those two lines the data type is int which is really an Int32 but you're doing a convert to Int16 . Next:

float mark = Convert.ToInt16(textBoxMarks.Text);

In that case your data type is a float but again you're converting to an Int16 .

How you could handle the insert:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace daniweb
  public partial class frmInsert : Form
    public frmInsert()

    private void button1_Click(object sender, EventArgs e)
      const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
      int regno = Convert.ToInt16(textBoxRegNOO.Text);
      int assignment = Convert.ToInt16(textBoxAssignmentNo.Text);
      string ass_topic = textBoxTopic.Text;
      float mark = Convert.ToInt16(textBoxMarks.Text);
      string query;
        List<string> Sql = new List<string>();
        Sql.Add("INSERT INTO assignmentMarks (regno,asno,mark) values (@RegNo,@AsNO,@Marks)");
        Sql.Add("insert into assignments (asno,as_topic) values (@RegNo,@Topic)");
        query = GetText(Sql);
      using (SqlConnection conn = new SqlConnection(connStr))
        using (SqlCommand cmd = new SqlCommand(query, conn))
          cmd.Parameters.Add(new SqlParameter("@RegNo", SqlDbType.SmallInt)).Value = regno;
          cmd.Parameters.Add(new SqlParameter("@AsNO", SqlDbType.SmallInt)).Value = assignment;
          cmd.Parameters.Add(new SqlParameter("@Topic", SqlDbType.VarChar)).Value = ass_topic;
          cmd.Parameters.Add(new SqlParameter("@Marks", SqlDbType.Float)).Value = mark;
          int rowCount = cmd.ExecuteNonQuery();

    internal static string GetText(List<string> sc)
      StringBuilder sb = new StringBuilder();
      for (int i1 = 0; i1 < sc.Count; i1++)
      return sb.ToString();

Thnxxx Ionelul , but in that example it talking about one table, i want to insert in to two tables

Edited by niro_fernando: n/a


woooow .Thanks sknake , i m still a student, i dont know anything about industry level,Does linq to sql good practice or a bad practice, Do industry use Linq to sql,i m practicing linq to sql


Its not good or bad practice its just a question of what you want to use. I use it for ASP.NET web applications because a number of third party components I use support paging with LINQ. Beyond that I try not to use it because of the uncertain future of the product. Another issue I have with LINQ is you can't get at the SQL transaction very easily. I have (NOLOCK) table hints in most of my select statements to get dirty reads / avoid deadlocking and the only way I can do that with LINQ is to create a view with table hints and select against the view. This makes it very cumbersome to use LINQ.

Please mark this thread as solved if you have found a solution to your question and good luck!

Edited by sknake: n/a

This question has already been answered. 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.