0

I have a gridview that shows all items under an order. I also have a Textbox I add for the user to add quantity. When they hit enter on that textbox it should run my writeTransaction.
Here is what the Gridview looks like: enter image description here

Here is my code for my transaction:

protected void writeTransaction()
{
    DateTime todaysDate = DateTime.Now;

    GridViewRow row = pickListGridview.SelectedRow;
    TextBox trQty = row.FindControl("TextBox1") as TextBox;
    string unitMeasure = row.Cells[6].Text;
    string itemNum = row.Cells[1].Text;
    string location = row.Cells[7].Text;
    string ordNum = orderNumTextBox.Text.ToUpper();

    OleDbCommand cmdWrite = new OleDbCommand("INSERT INTO TRDATA (ACREC, ACSEQ, ADJDT, ADJTM ,APCOD, APHMS, APRJC, BADGE, BLKSQ, CONO, CRWYN, CTLID, EFFIC, ENTUM, " +
        "EXFLG, FDATE, IPLOC, ITNBR, LBTIM, LCQC2, LINSQ, LLOCN, LNKNO, LPQC1, MATIM, MSCST, MSQTY, MUCST, MUQTY, ODUDT, ORDNO, PARNT, PLIST, PNREF, QCNTR, QPIEC, QUEUE," +
        " RJQTY, RLIST, RNREF, RSUPF, SCRAP, SDATE, SELYN, SEQNM, SHFTC, TCOST, TDATE, TRAMT, TRFMT, TRNA2, TRNN2, TRNC2, TRNNO, TRNNOO, TRQTY, TSTAT, TSTATS, TTIME, TURFA, " +
        "TURFN, TURFC, TURNA, TURNN, TURNC, USER2, VRQTY, TRFG, ESHR, WTSK) " +
        $"VALUES('Y', '0', '0', '0', 'I', '0', '0', '4153', '0', '0', 'N', '*', '0', '{unitMeasure}', '0', '1141208', '1', '{itemNum}', '0', '0', '0', '{location}', '0', '0', " +
        $"'0', '0', '0', '0', '0', '0', '{ordNum}', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '29', '1', '0', '1171103', '0', 'IP', '0'," +
        $" '0', '0', '8888889', '0', '{trQty.ToString()}', '2', '0', '85409', '0', '0', '0', '2', '8728517', '5', 'PPL000000017017', '0', '0', '0', '0')", cnTrData);

    cnTrData.Open();
    cmdWrite.ExecuteNonQuery();
    cnTrData.Close();
}

Here is my code for my gridview

<asp:GridView ID="pickListGridview" runat="server" class="hoverTable" OnRowDataBound="pickListGridview_DataBound" OnSelectedIndexChanged="OnSelectedIndexChanged" OnRowCreated="deleteCol"  >
        <Columns>
            <asp:TemplateField HeaderText="QTY">
                <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text="" Width="60px" onkeypress="return EnterEvent(event)" ReadOnly="true"></asp:TextBox>
                 </ItemTemplate>
                <ItemStyle Width="25px" HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Here is the javascript that runs the ENTER key command which fires the Save button click event.

<script type="text/javascript">
    function EnterEvent(e) {
        if (e.keyCode == 13) {
            __doPostBack('<%=saveButton.UniqueID%>', "");
        }
    }
</script>

Here is my click event

protected void saveButton_Click(object sender, EventArgs e)
{
    //Response.Write("<script>alert('You have overpicked! This is just a warning!');</script>");
    writeTransaction();       
}

I know the ENTER button click event fires fine because when I do that and uncomment the Response.Write it fires just fine. I think the issue is lying in the transaction piece because I get an Object not found. Is that SelectedRow the right way to do that? I can do a foreach for the entire gridview and it works fine but I want to do it line by line. Any help would be appreciated!

  • Are you trying to use an interpolated string without the $ character at the beginning of the interpolated string? Also you need the verbatim character @ to split that command on more than one line. See here https://stackoverflow.com/questions/31638579/how-do-you-use-verbatim-strings-with-interpolation – Steve Nov 03 '17 at 16:35
  • I get the error before that even fires... it errors out on the TextBox and if I comment that out it errors on the unitMeasure line. Like it's not finding them. – ksuProgrammer Nov 03 '17 at 16:37
  • 1
    But what error? Could you explain this very important detail? – Steve Nov 03 '17 at 16:38
  • System.NullReferenceException: Object reference not set to an instance of an object. is what I get when it goes to the TextBox line – ksuProgrammer Nov 03 '17 at 16:39
  • 1
    So row is Null. There is no SelectedRow – Steve Nov 03 '17 at 16:40
  • Right... so my question was if the code gridview.SelectRow isn't working what do I have to do to get those values from the row that textbox is in? – ksuProgrammer Nov 03 '17 at 16:41
  • What if there are multiple rows? Selectedrow is the one which is highlighted (none in your case, hence the error). – Jacob H Nov 03 '17 at 16:44
  • This should be only one row. So what is the best approach to doing that? Basically they put in the quantity and hit enter it writes out a transaction solely for that item they are on. – ksuProgrammer Nov 03 '17 at 16:46
  • picklistgridview.Rows[0] should work if there is only a possibility of a single row. – Jacob H Nov 03 '17 at 16:51
  • Right but what if there is 20 rows and I want only the row I clicked in the textbox? Thats why I thought of the selected row. – ksuProgrammer Nov 03 '17 at 16:55
  • For each loop through all rows beforehand to find the textbox control and then set your row variable maybe? I'm not sure if there's a better way, at least with what information we have. Maybe someone has a different approach. – Jacob H Nov 03 '17 at 16:59
  • Maybe another variable containing the row index triggered through the onselectedindexchanged event? Then you can use the index to isolate the row. – Jacob H Nov 03 '17 at 17:04

0 Answers0