-3

I have the following TextBox and Button:

<asp:TextBox ID="input" runat="server" Height="10px" Width="60px">123456</asp:TextBox>

<asp:Button ID="Button1" runat="server" OnClick="ViewButton_Click" Text="Button" />

The TextBox has a default value of '123456'. Clicking the botton should then trigger the following:

public byte[] GetImageData(int ID)
    {

     ....

        SqlCommand command = new SqlCommand("select FileImage from TestServ.dbo.Tickets WHERE (FileType = 'PDF' AND ID = 111111)", connection);

     ....

    }

Currently, the ID is manually set to '111111', which will be used to grab a PDF from a server corresponding to the ID entered. What I'm having trouble doing however is modifying the code so that it grabs the ID I enter into the 'input' TextBox (currently 123456) and then sets it as the ID in the above code rather than '111111'.

rene
  • 41,474
  • 78
  • 114
  • 152
JimmyK
  • 4,801
  • 8
  • 35
  • 47
  • I think http://stackoverflow.com/questions/2377506/pass-array-parameter-in-sqlcommand is the same. – nycdan Oct 24 '13 at 14:23

2 Answers2

4

You could simply try to convert the content of the input textbox to an integer using the TryParse method and then call the GetImageData method passing the converted value.

Of course, in GetImageData you use a parameterized query to pass the ID as a parameter to the framework code that queries the database

public byte[] GetImageData(int ID)
{

    string cmdText = "select FileImage from TestServ.dbo.Tickets " + 
                     "WHERE (FileType = 'PDF' AND ID = @id)";
    SqlCommand command = new SqlCommand(cmdText, connection);
    command.Parameters.AddWithValue("@id", ID);

    ....

}

calling code.....

int ID;
if(Int32.TryParse(input.Text, out ID))
    byte[] result = GetImageData(ID);

else

  .... show an error ....
Steve
  • 213,761
  • 22
  • 232
  • 286
1

You pass use parameterized query like

SqlCommand command = new SqlCommand("select FileImage from TestServ.dbo.Tickets WHERE (FileType = 'PDF' AND ID = @ID)", connection);
command.Parameters.AddWithValue("@ID", input.Text);

Note: Assuming GetImageData function is in code behind

Satpal
  • 132,252
  • 13
  • 159
  • 168