Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do error checking before you insert data.</p> <p>That error is telling you that <code>txtRef.Text</code> does not have a value, but that a value is required.</p> <p>Once you finish error checking for the <code>Ref</code> value, you should carry on through all of your other values as well.</p> <p>You don't want to create a mess in your database.</p> <p><strong>UPDATE:</strong></p> <p>Make sure your <code>GridView</code> only updates the correct row types.</p> <p>I do not really like using the <code>AddWithValue</code> tool, because it seems like it prevents people from understanding their database. Your <code>Add(new SqlParameter(</code> techniques essentially are doing just that.</p> <p>For example, these two lines produce identical results:</p> <pre><code>cmd.Parameters.AddWithValue("@Ref", txtRef.Text); cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text)); </code></pre> <p>What is <code>@Ref</code> in your database? <code>Integer</code>? <code>varchar(50)</code>? Obviously, there would be a big difference in how the database would treat the two.</p> <p>If it were an <code>integer</code>, it should really be written as:</p> <pre><code>cmd.Parameters.Add("@Ref", SqlDbType.Int).Value = Convert.ToInt32(txtRef.Text.Trim()); </code></pre> <p>If it were a <code>varchar(50)</code>, it should really be written as:</p> <pre><code>cmd.Parameters.Add("@Ref", SqlDbType.VarChar, 50).Value = txtRef.Text.Trim(); </code></pre> <p>Since I do not know anything about your database, I was left using <code>AddWithValue</code>, but I would strongly recommend looking at the design of your database tables, then match up your <code>SqlParameters</code> to how they are designed.</p> <p>I'd also suggest using a <code>const</code> SQL Connection String, since your database will not be changing while your program is using it:</p> <pre><code>private static string m_sqlConn = "Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"; </code></pre> <p>I also created a <code>static</code> <strong>DbSafe</strong> method to "message" the data before it gets inserted. A method dedicated to this allows you to easily channel all of your data to make sure it is safe. Also, modifying the method later to add more functionality is easy to do and instantly translates to all of the code you use it in.</p> <pre><code>private static object DbSafe(object value) { if ((value != null) || (value != DBNull.Value)) { string strVal = value.ToString(); if (!String.IsNullOrEmpty(strVal)) { return strVal.Trim(); } } return DBNull.Value; } </code></pre> <p>There also is no reason for you to be making two (2) separate SQL objects (<code>SqlCommand</code> and <code>SqlDataAdapter</code>) to perform your task.</p> <p>Here is how I wrote my modified version:</p> <pre><code>protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { if (GridView1.Rows[e.RowIndex].RowType == DataControlRowType.DataRow) { string index = GridView1.Rows[e.RowIndex].Cells[1].Text; //DwgRegID string sqlUpdate = "UPDATE dbo.Dwg_Register SET " + "Ref=@Ref, Dwg_Ref=@Dwg_Ref, Title=@Title, Dwg_Received_Date=@Dwg_Received_Date, " + "Rev=@Rev, Trade=@Trade, type1=@type1, Produced_Date=@Produced_Date, " + "Produced_By=@Produced_By, Submittal_Ref=@Submittal_Ref, Issued_To=@Issued_To, " + "Date_Issued=Date_Issued, Purpose=@Purpose, status1=@status1, Action_Date=@Action_Date " + "WHERE DwgRegID=N'" + index + "'"; string sqlSelect = "SELECT DwgRegID, Ref, Dwg_Ref, Title, Dwg_Received_Date, Rev, Trade, type1, Produced_Date, Produced_By, Submittal_Ref, " + "Issued_To, Date_Issued, Purpose, status1, Action_Date " + "from dbo.Dwg_Register"; var table = new DataTable(); using (var cmd = new SqlCommand(sqlUpdate, new SqlConnection(m_sqlConn))) { cmd.Parameters.AddWithValue("@Ref", DbSafe(txtRef.Text)); cmd.Parameters.AddWithValue("@Dwg_Ref", DbSafe(txtDwgRef.Text)); cmd.Parameters.AddWithValue("@Title", DbSafe(txtTitle.Text)); cmd.Parameters.AddWithValue("@Dwg_Received_Date", DbSafe(txtDwgReceivedDate.Text)); cmd.Parameters.AddWithValue("@Rev", DbSafe(txtRev.Text)); cmd.Parameters.AddWithValue("@Trade", DbSafe(ddlTrade.Text)); cmd.Parameters.AddWithValue("@type1", DbSafe(ddlType.Text)); cmd.Parameters.AddWithValue("@Produced_Date", DbSafe(txtProducedDate.Text)); cmd.Parameters.AddWithValue("@Produced_By", DbSafe(ddlProducedBy.Text)); cmd.Parameters.AddWithValue("@Submittal_Ref", DbSafe(txtSubmittalRef.Text)); cmd.Parameters.AddWithValue("@Issued_To", DbSafe(ddlIssuedTo.Text)); cmd.Parameters.AddWithValue("@Date_Issued", DbSafe(txtDateIssued.Text)); cmd.Parameters.AddWithValue("@Purpose", DbSafe(ddlPurpose.Text)); cmd.Parameters.AddWithValue("@status1", DbSafe(ddlStatus.Text)); cmd.Parameters.AddWithValue("@Action_Date", DbSafe(txtActionDate.Text)); //cmd.Parameters.Add(new SqlParameter("@DwgRegID", index)); try { cmd.Connection.Open(); cmd.ExecuteNonQuery(); cmd.CommandText = sqlSelect; cmd.Parameters.Clear(); table.Load(cmd.ExecuteReader()); } catch (SqlException err) { Response.Write(err.Message); } finally { cmd.Connection.Close(); } } GridView1.DataSource = table; GridView1.DataBind(); MultiView1.SetActiveView(ViewGrid); lblUpdate.Text = "Record updated sucessfully."; } } </code></pre> <p>Further, you will notice above that I added a <code>try/catch/finally</code> block that only catches <code>SqlException</code> errors, then it immediately closes the database connection.</p> <p>I'm not sure what your <code>GridViewEditEventArgs</code> is doing and/or if it hurts anything, so I left it alone.</p> <p>I hope that helps. It took entirely too long to explain.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload