-3

I have two tables in a SQL Server database. I select from table ADMS and I need to insert master table by gridview but I dont know how to insert with gridview. Please help. I've tried for many days and I did not pass yet

protected void Button3_Click1(object sender, EventArgs e)
{
    if (RadioButton2.Checked)
    {
        SqlConnection con = new SqlConnection(MyConnectionString);
        // con.Open(); // don't need the Open, the Fill will open and close the connection automatically
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ADMS_Machining where datetime='" + TextBox1.Text + "'", con);
        mytable = new DataTable();
        da.Fill(mytable);
        GridView2.DataSource = mytable;
        GridView2.DataBind();
    }
    else
    {
        SqlConnection con = new SqlConnection(MyConnectionString);
        // con.Open(); // don't need the Open, the Fill will open and close the connection automatically
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Machining_Master where datetime='" + TextBox1.Text + "'", con);
        mytable = new DataTable();
        da.Fill(mytable);
        GridView2.DataSource = mytable;
        GridView2.DataBind();
    }
}

protected void Button4_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    String strConnString, strSQL;

    strConnString = "Server=kane-pc;UID=sa;PASSWORD=1234;Database=Machining;Max Pool Size=400;Connect Timeout=600;";
   //here

   conn.ConnectionString = conn;
   conn.Open();
   cmd.Connection = conn;
   cmd.CommandText = strSQL;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kane smith
  • 25
  • 1
  • 9

2 Answers2

1

You can extract values from a grid view depending on what you have placed in the cells...

string value = this.GridView2.Rows[0].Cells[0].Text;

You can also track the selected row event, and get specific controls like the following...

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{  
    string someValueTakenFromLabel = (GridView2.SelectedRow.FindControl("lblAnyLabelHere") as Label).Text;

    // .... do something with value here
}

I suggest you go through some tutorials though to get the hang of how to use GridView.

Mez
  • 4,666
  • 4
  • 29
  • 57
0

You have to first read data from cells and then insert them into database using SqlCommand. Assuming that you have M_ID and M_NAME columns in your Machining_Master table you can insert values to database as below:

//Assuming that your id column is first column and name is second column
//get value of id and name
int mId = Convert.ToInt32(GridView2.SelectedRow.Cells[0].Text);
string mName = GridView2.SelectedRow.Cells[1].Text;

string connectionStrng = "your connection string";
string insertSql = "INSERT INTO Machining_Master (M_ID, M_NAME) VALUES (@mId, @mName)";
using (SqlConnection conn = new SqlConnection(connectionStrng))
{
    using (SqlCommand cmd = new SqlCommand(insertSql, conn))
    {
        try
        {
            cmd.Parameters.Add(new SqlParameter("mId", mId));
            cmd.Parameters.Add(new SqlParameter("mName", mName));

            conn.Open();
            cmd.ExecuteNonQuery();
        }
        finally
        {
            //Close connection
            conn.Close();
        }
    }
}
Adil Mammadov
  • 8,476
  • 4
  • 31
  • 59
  • have error {"Object reference not set to an instance of an object."} – kane smith Jan 09 '15 at 09:55
  • Double DailyMean = Convert.ToDouble(GridView2.SelectedRow.Cells[0].Text); Double Process_Mean = Convert.ToDouble(GridView2.SelectedRow.Cells[1].Text); Double Long_Term_Stdev = Convert.ToDouble(GridView2.SelectedRow.Cells[2].Text); Double CPK_ESTIMATE = Convert.ToDouble(GridView2.SelectedRow.Cells[3].Text); Double DAILY_MINIMUM = Convert.ToDouble(GridView2.SelectedRow.Cells[4].Text); Double DAILY_MAXIMUM = Convert.ToDouble(GridView2.SelectedRow.Cells[5].Text); – kane smith Jan 09 '15 at 09:56
  • have you selected a row? – Adil Mammadov Jan 09 '15 at 10:22